YUI3学习(三)---OOP

24 篇文章 0 订阅
10 篇文章 0 订阅

前一篇 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'
 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值