B.hasOwnProperty(A) 判断A是不是B的一个属性或对象。
浅拷贝
在更改新的对象的值时,原先的对象的值也就跟着改变
$.jQuery.extend(Css1)
Copy = (parent,NewObj)=>{
for( i in parent ){
if(parent.hasOwnProperty(i)){
child[i]=parent[i]
}
}
return child;
}
深拷贝
新生成的对象的值的改变 不会影响原来的对象 各自独立
function
extendDeep(parent, child) {
var
i,
proxy;
proxy = JSON.stringify(parent);
//把parent对象转换成字符串
proxy = JSON.parse(proxy)
//把字符串转换成对象,这是parent的一个副本
child = child || {};
for
(i
in
proxy) {
if
(proxy.hasOwnProperty(i)) {
child[i] = proxy[i];
}
}
proxy =
null
;
//因为proxy是中间对象,可以将它回收掉
return
child;
}
另一种深拷贝
var cloneObj = function(obj){
var str, newobj = obj.constructor === Array ? [] : {};
if(typeof obj !== 'object'){ return; }
else if(window.JSON){
str = JSON.stringify(obj), //系列化对象 newobj = JSON.parse(str); //还原
}
else { for(var i in obj){ newobj[i] = typeof obj[i] === 'object' ? cloneObj(obj[i]) : obj[i]; } } return newobj;};