kuangxiangly 发表于 2013-1-26 12:58:20

[Extjs项目开发必须知道的技术之一]--apply和applyif

      目前本人在做公司项目的前台框架的升级工作,前台框架使用的是Extjs2,目前需要将框架升级到Extjs4。虽然之前使用Extjs大概快2年啦,但是因为一直没有太多的时间对一些知识点或者关键技术进行深入研究,本人也想写一点自己的积累或者见解,一直也无从下手。所以这次就以升级框架作为契机,深入的对ext的技术点进行研究,希望与大家一起分享。在这个过程中我也会把一些经常遇到知识进行归类、整理、总结。
Extjs项目开发必须知道的技术之一:apply和applyif
apply方法:apply( Object obj, Object config, Object defaults );
第一个参数是要拷贝的目标对象,
第二个参数是拷贝的源对象,
第三个参数是可选的,表示给目标对象提供一个默认值。
 
可以简单的理解成把第三个参数(如果有的话)及第二个参数中的属性拷贝给第一个参数对象。

Ext源代码如下:

 


 
view plaincopy to clipboardprint?/**   * Copies all the properties of config to obj.   * @param {Object} obj The receiver of the properties   * @param {Object} config The source of the properties   * @param {Object} defaults A different object that will also be applied for default values   * @return {Object} returns obj   * @member Ext apply   */Ext.apply = function(o, c, defaults){       // no "this" reference for friendly out of scope calls       if(defaults){         Ext.apply(o, defaults);       }       if(o && c && typeof c == 'object'){         for(var p in c){               o = c;         }       }       return o;   };/** * Copies all the properties of config to obj. * @param {Object} obj The receiver of the properties * @param {Object} config The source of the properties * @param {Object} defaults A different object that will also be applied for default values * @return {Object} returns obj * @member Ext apply */Ext.apply = function(o, c, defaults){    // no "this" reference for friendly out of scope calls    if(defaults){      Ext.apply(o, defaults);    }    if(o && c && typeof c == 'object'){      for(var p in c){            o = c;      }    }    return o;};  
 
applyif 也是对象克隆,不同的是,克隆的对象并不会覆盖原有属性和方法 
具体代码如下:
view plaincopy to clipboardprint?applyIf : function(o, c){             if(o){               for(var p in c){                     if(!Ext.isDefined(o)){                         o = c;                     }               }             }             return o;         },  

 
 
页: [1]
查看完整版本: [Extjs项目开发必须知道的技术之一]--apply和applyif