js框架设计1.2对象扩展笔记

需要一个新的功能添加到我们的命名空间上。这方法在JS中被叫做extend或者mixin,若是遍历属性用一下1.1代码,则会遍历不出原型方法,所以1.2介绍的是mass Framework里的mix方法。

类似juqery的$.extend()。

 

1.1代码

function extend(destination,source){
  for(var property in source)
    destination[[property] = source[property];
  return destination;
}

 

1.2mix方法

fucntion mix(target,source){

  var args=[].slice.call(arguments),i-1,key,ride=type of args[args.length-1]==‘boolean‘?args.pop():true;

  if(arg.length===1){

    target = !this.window?this:{};

    i=0; 

  }

  while((source = args[i++])){

    for(key in source){

       if(ride || !(key in target)){

         traget[key]=source[key];

   }

    }
   return target;

  }

}

 

 

js框架设计1.2对象扩展笔记,布布扣,bubuko.com

js框架设计1.2对象扩展笔记

上一篇:JS魔法堂:追忆那些原始的选择器


下一篇:jQuery对象与dom对象的相互转换