ext.apply
extjs的APPly,applyIf和merge都用来操作2个对象并生成新对象,在实现结果上是有区别的,下面来看下他们的区别:
先定义2个对象:
var obj1 = {
a: '语文',
b: ['美术1', '美术2'],
c: true,
d: {
d1: 2000,
d2: '你好',
d3: true,
d4: {
d41:200,
d42:100
}
}
};
var obj2 = {
a: '数学',
b: ['美术1', '美术2', '美术3', '美术4'],
d: {
d1: 40000,
d2: '你坏',
d4: {
d41:100,
d43:800,
},
d5: 'lala'
},
e: 100
};
1、apply方法
var newobj=ext.apply(obj1,obj2);
console.log('obj1='+JSON.stringify(obj1));
console.log('obj2='+JSON.stringify(obj2));
console.log('newobj='+JSON.stringify(newobj));
结果如下:
obj1={"a":"数学","b":["美术1","美术2","美术3","美术4"],"c":true,"d":{"d1":40000,"d2":"你坏","d4":{"d41":100,"d43":800},"d5":"lala"},"e":100}
obj2={"a":"数学","b":["美术1","美术2","美术3","美术4"],"d":{"d1":40000,"d2":"你坏","d4":{"d41":100,"d43":800},"d5":"lala"},"e":100}
newobj={"a":"数学","b":["美术1","美术2","美术3","美术4"],"c":true,"d":{"d1":40000,"d2":"你坏","d4":{"d41":100,"d43":800},"d5":"lala"},"e":100}
apply的方法的源代码:
Ext.apply = function(q, p, s) {
if (s) {
Ext.apply(q, s)
}
if (q && p && typeof p === "object") {
var r, o, n;
for (r in p) {
q[r] = p[r]
}
if (l) {
for (o = l.length; o--;) {
n = l[o];
if (p.hasOwnProperty(n)) {
q[n] = p[n]
}
}
}
}
return q
};
从代码中可以看出apply方法处理对象的多层属性,
obj1和obj2都有的属性,obj2属性值覆盖obj1
obj1有而obj2没有的属性,顶层保留属性,顶层以下删除属性
obj1没有而obj2有的属性,obj1新增属性
2、applyIf方法
var newobj=Ext.applyIf(obj1,obj2);
console.log('obj1='+JSON.stringify(obj1));
console.log('obj2='+JSON.stringify(obj2));
console.log('newobj='+JSON.stringify(newobj));
结果如下:
obj1={"a":"语文","b":["美术1","美术2"],"c":true,"d":{"d1":2000,"d2":"你好","d3":true,"d4":{"d41":200,"d42":100}},"e":100}
obj2={"a":"数学","b":["美术1","美术2","美术3","美术4"],"d":{"d1":40000,"d2":"你坏","d4":{"d41":100,"d43":800},"d5":"lala"},"e":100}
newobj={"a":"语文","b":["美术1","美术2"],"c":true,"d":{"d1":2000,"d2":"你好","d3":true,"d4":{"d41":200,"d42":100}},"e":100}
applyIf的方法的源代码:
applyIf: function(o, n) {
var p;
if (o) {
for (p in n) {
if (o[p] === undefined) {
o[p] = n[p]
}
}
}
return o
},
从代码中可以看出applyIf方法只处理对象的顶层属性,,顶层以下属性照抄obj1
顶层obj1没有而obj2有的属性,obj1新增属性,其他属性照抄obj1
3、merge方法
var newobj=Ext.merge(obj1,obj2);
console.log('obj1='+JSON.stringify(obj1));
console.log('obj2='+JSON.stringify(obj2));
console.log('newobj='+JSON.stringify(newobj));
结果如下:
obj1={"a":"数学","b":["美术1","美术2","美术3","美术4"],"c":true,"d":{"d1":40000,"d2":"你坏","d3":true,"d4":{"d41":100,"d42":100,"d43":800},"d5":"lala"},"e":100}
obj2={"a":"数学","b":["美术1","美术2","美术3","美术4"],"d":{"d1":40000,"d2":"你坏","d4":{"d41":100,"d43":800},"d5":"lala"},"e":100}
newobj={"a":"数学","b":["美术1","美术2","美术3","美术4"],"c":true,"d":{"d1":40000,"d2":"你坏","d3":true,"d4":{"d41":100,"d42":100,"d43":800},"d5":"lala"},"e":100}
merge方法处理对象的多层属性,
obj1和obj2都有的属性,obj2属性值覆盖obj1
obj1有而obj2没有的属性,保留属性
obj1没有而obj2有的属性,obj1新增属性
比较后发现apply,applyIf和merge方法执行后newobj会覆盖obj1,他们的值是一样的。
相关阅读
sscanf的作用:从一个字符串中读进于指定格式相符的数据。利用它可以从字符串中取出整数、浮点数和字符串。 sscanf和scanf的区别:sc
Bundle类介绍: Bundle是一个载体,可以存放基本数据类型、对象等内容,相当于一辆汽车,可以装载很多东西,然后运到需要的
1,序 具体如何安装,以及安装什么版本,此处就不细细和大家说了,此处我们认为最新版即最稳定,用的人也最多,认可度最广泛,无论何时你
一、什么是JavaBean JavaBean是一个遵循特定写法的Java类,它通常具有如下特点: 这个Java类必须具有一个无参的构造函数 属性必须
MySQL中TIMESTAMPDIFF和TIMESTAMPADD函数的用法
一,TIMESTAMPDIFF 语法: TIMESTAMPDIFF(interval,datetime_expr1,datetime_expr2) 说明: 返回日期或日期时间表达式datetime_expr