zoukankan      html  css  js  c++  java
  • JS原型一些方法

    看图说话 栗子子在这里

    var obj = {
      person: {
        name: 'joe',
        history: {
          hometown: 'bratislava',
          bio: {
            funFact: 'I like fishing.'
          }
        }
      }
    };
    
    obj.hash('person.name'); // 'joe'
    obj.hash('person.history.bio'); // { funFact: 'I like fishing.' }
    obj.hash('person.history.homeStreet'); // undefined
    obj.hash('person.animal.pet.needNoseAntEater'); // undefined

    关于原型的方法好像都不会哎   直接看答案啊!

    best  practics

    Object.prototype.hash = function(string) {
      var arr = string.split('.');
      return arr.reduce(function(pv, cv){
        return (pv) ? pv[cv] : pv;
      }, this);
    }
    
    
    //reduce 归并方法  四个参数 (pre next index array);
    //返回值作为第一的参数传给下一项,第一次迭代发生在数组第二项上,第一个参数是数组第一项,第二项是数组第二项

    reduce()方法再啰嗦两句 ,好像很牛逼的样子 

    少年,我看你很有想法,来和我求和吧

    var arr=[1,2,3,4,5];
    m=arr.reduce(function(pre,next,index,this){
    return pre+next;
    });

    alert(m);

    //理解下上面的代码

    还有个能看懂的理解下

    Object.prototype.hash = function(string) {
      var current = this;
      var props = string.split('.');
      
      while (current && props.length) {
        current = current[props.shift()];
      }
      
      return current;
    }
    
    //shifit()方法  移除队列首项,和push  相反
  • 相关阅读:
    触摸屏网站开发系列(一)-ios web App应用程序(ios meta)
    jQuery Mobile 入门教程
    html的视频插件 (转)
    网页布局流式布局
    2.05-random-uesr-proxy
    2.04-proxy-handler
    2.03-handler_openner
    2.02-request_header_two
    2.01-request_header
    1.03-get_params2
  • 原文地址:https://www.cnblogs.com/liuestc/p/4525730.html
Copyright © 2011-2022 走看看