1.对象的继承扩展——深度复制
Object.prototype.extend = function(...rest){
//使用es6中的剩余参数方式来取代之前arguments
var i=0,
len = rest.length;
for(i; i<len; i++){
var source = rest[i];
for(var property in source){
//通过hasOwnProperty的判断来避免操作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;
}
网友评论