1.對(duì)象的繼承擴(kuò)展——深度復(fù)制
Object.prototype.extend = function(...rest){
//使用es6中的剩余參數(shù)方式來(lái)取代之前arguments
var i=0,
len = rest.length;
for(i; i<len; i++){
var source = rest[i];
for(var property in source){
//通過(guò)hasOwnProperty的判斷來(lái)避免操作prototype上的屬性
if(source.hasOwnProperty(property)){
var sourceTmp = {};
if(typeof source[property] == "object"){
var tmp = Array.isArray(source[property]) ? [] : {};
sourceTmp = tmp.extend(source[property]);
} else {
sourceTmp = source[property];
}
this[property] = sourceTmp;
}
}
}
return this;
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者