`
houfeng0923
  • 浏览: 142542 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

YUI3学习(三)---OOP

阅读更多

前一篇 YUI3学习(二)--YUI Global Object

在前篇YUI3 Global Object中介绍了oop模块中的的两个继承相关的方法 extend 和augment。

本篇介绍下YUI3 oop模块剩余的几个方法。

 

Y.aggregate(r,s,ov.wl)

同样的基于Y.mix的属性合并方法;需要  区别Y.aggregate与Y.merge 

API说明如下:

 

object aggregate ( r , s , ov , wl )

Applies object properties from the supplier to the receiver. If the target has the property, and the property is an object, the target object will be augmented with the supplier's value. If the property is an array, the suppliers value will be appended to the target. 
如果属性是object,则根据是否覆盖参数ov,进行属性合并操作;
如果属性是array,并且ov参数为false,则根据数组索引合并属性
 如果ov参数为true,则会使用s的数组覆盖

 

   源码如下:

Y.aggregate = function(r, s, ov, wl) {
        return Y.mix(r, s, ov, wl, 0, true);
};

 

   示例代码:

 

   var s={
	     'a':'aaaa', 'b':[1,2,3]
   };
   var r = {
   	'a':'rrrr', 'b':[4]
   };
   
    Y.mix(r,s);  
   alert(Y.dump(r))//{a=>rrrr,b=>[4]}
//------------------------------------
   var s2 = {
   	'a':'aaaa', 'b':[1,2,3]
   };
   var r2 = {
   	'a':'rrrr', 'b':[4]
   };
      
   Y.aggregate(r2,s2); //等效: Y.mix(r2,s2,false,[],0,true); 
  alert(Y.dump(r2)) //{a=>rrrr,b=>[4,2,3]}
  Y.aggregate(r2,s2,true);等效:Y.mix(r2,s2,true,[],0,true);
   alert(Y.dump(r2));//{a=>aaaa,b=>[1,2,3]}
 

 

Y.clone(o,safe,func,context,owner,cloned)

详细api参考   返回对象深度克隆。

其中参数safe==true,不会克隆prototype属性; 否则克隆所有,这时,改变 prototype属性会对原始和克隆对象造成影响。

 

var o = {
	'a':[1,2], 'b':{'name':'b1'}
}
var clonedObj = Y.clone(o);
o.a.push(3);
o.b.name = 'b1-changed';
alert(clonedObj.a) //1,2
alert(clonedObj.b.name);//b1
 

 

Y.bind(func,context,args*)

返回绑定上下文和参数值的函数。 (参数可选)

示例代码: 

 

function Chinese(){
}
Chinese.prototype.say = function(name,msg){
	alert('你好:'+name+','+msg);
}
function sayHello(name,msg){
	//.....
	this.say(name,msg);
}

var bindedFun = Y.bind(sayHello,new Chinese(),'YUI3');
bindedFun('i like you'); //'你好:YUI3,i like you'
 

 

 

Y.rbind(func,context,args*)

与Y.bind相似,返回绑定上下文和参数值的函数;不同的是绑定参数的顺序。

示例代码:(注意与Y.bind的区别)

 

function Chinese(){
}
Chinese.prototype.say = function(name,msg){
	alert('你好:'+name+','+msg);
}
function sayHello(name,msg){
	//.....
	this.say(name,msg);
}

var rbindedFun = Y.rbind(sayHello,new Chinese(),'i like you');
rbindedFun('YUI3'); //'你好:YUI3,i like you'
 

 

 

 

 

 

 

 

 

 

 

  • 大小: 3.2 KB
2
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics