這里,梳理5種克隆對象的方法涯贞,各自有所偏重枪狂,君可按喜好摘取~
使用JQuery
淺克隆: var newObj = $.extend ({},oldObj);
深克滤斡妗: var newObj = $.extend (true,{},oldObj);
使用JSON方法(深克隆)
var newObj = JSON.parse (JSON.stringify (oldObj) )
特點:(1) 要求克隆的對象是JSON-safe的州疾;(2) 無法克隆對象內(nèi)部的函數(shù)指針
eg:
var oldObj = {
name: 'Candy 程'
favor: function () {
alert('I will back!')
}
}
var newObj = JSON.parse (JSON.stringify (oldObj) ) //{ name: 'Candy 程' }
ES6 Objtct.assign({},oldObj)
特點:無法克隆對象屬性的特定描述符,克隆后恢復(fù)默認(rèn)描述符
eg:
var oldObj = {}
Object.defineProperty (oldObj, "name", {
value: 'Candy 程',
writable: false,
configurable: false,
enumerable: true
});
old.favor = function () {
alert("I will back!")
}
var newObj = Object.assign({},oldObj)
Object.getOwnPropertyDescriptor(newObj,"name") //屬性為可枚舉類型時方法有效
/*{
value: 'Candy 程',
writable: true,
configurable: true傻谁,
enumerable: true
}
*/
使用for...in
特點:由于使用for...in,使得(1) 將原型上的屬性也添加到新對象上孝治;(2) 不能克隆非枚舉屬性列粪;(3) 涉及到調(diào)用自身
Object.prototype.clone = function(){
var newObj;
if (this.constructor == Object){
newObj = new this.constructor();
}else{
newObj = new this.constructor(this.valueOf());
}
for(var key in this){
if ( newObj[key] != this[key] ){
typeof(this[key]) === 'object' ? this[key].clone() : this[key]
}
}
//修復(fù)for..in無法遍歷到toString审磁、valueOf方法的缺陷
newObj.toString = this.toString;
newObj.valueOf = this.valueOf;
return newObj;
}
使用Object.getPropertyNames()
特點:由于使用Object.getPropertyNames(),使得(1) 不克隆原型屬性(2) 克隆自身所有屬性岂座,包括非枚舉屬性
Object.prototype.clone=function(){
//原型指向保持一致
var newobj=Object.create(Object.getPrototypeOf(this))
//自身屬性保持一樣
var propNames=Object.getOwnPropertyNames(this)
propNames.forEach(function(item){
//保持每個屬性的特性也一樣
var des=Object.getOwnPropertyDescriptor(this,item);
Object.defineProperty(newobj,item,des)
},this)
return newobj
}
拓展:
for...in 可遍歷到原型屬性态蒂,但無法讀到enumerable: false(非枚舉)的屬性;
Object.keys() 返回對象的所有自身可枚舉屬性名組成的數(shù)組费什,但不包括原型中的屬性;
Object.getOwnPropertyNames() 返回對象的所有自身屬性名(包括不可枚舉的屬性)組成的數(shù)組钾恢,但不會獲取原型鏈上的屬性;