对象拷贝

可遍历属性

  • 浅拷贝
if(typeof Object.prototype.copy != "function") {
    Object.prototype.copy = function () {
        var obj = {};
        for(var i in this) {
            this.hasOwnProperty(i) ? obj[i] = this[i] : false;
            //if(this.hasOwnProperty(i)) {
            //    obj[i] = this[i];
            //}
            
        }
        return obj
    }
}
var o ={a : 1, b : 2,c : {c : 3}};
console.log(o.copy());

简单的浅拷贝,可以用Object.assign,对存取器定义的对象也试用

var o ={a : 1, b : 2,c : {d : {get t() {return 123}}}};  
console.log(Object.assign(o));
  • 深拷贝
if(typeof Object.prototype.deepCopy != "function") {
    Object.prototype.deepCopy = function () {
        var obj = {};
        for(var i in this) {
            if(this.hasOwnProperty(i)) {
                if(typeof this[i] === "object") {
                    obj[i] = this[i].deepCopy()
                }
                else{
                    obj[i] = this[i];
                }
                
            }
            //this.hasOwnProperty(i) ? obj[i] = this[i] : false;
        }
        return obj
    }
}
var o ={a : 1, b : 2, c : {d : {e : 4}}};

var deepO = o.deepCopy();
//o.c.d.e = 5;
o.c.d = 3;
console.log(deepO);       //{a : 1, b : 2, c : {d : {e : 4}}}
console.log(o);           //{a : 1, b : 2, c : {d : 3}}

属性描述对象的拷贝

这是个浅拷贝

var o = {get c() {return 123}};
if(typeof Object.prototype.extend != "function") {
    Object.prototype.extend = function () {
        var obj = {};
        for(var property in this) {
            if(this.hasOwnProperty(property)) {
                Object.defineProperty(
                    obj, 
                    property, 
                    Object.getOwnPropertyDescriptor(this, property)
                );
            }

        }    
        return obj;
    }
}

var cpo = o.extend();
console.log(cpo);

深拷贝

var o = {a : 1, b : 2,c : {d : {get t() {return 123}}}};
if(typeof Object.prototype.deepExtend != "function") {
    Object.prototype.deepExtend = function () {
        var obj = {};
        for(var property in this) {
            if(this.hasOwnProperty(property)) {
                if(typeof this[property] === "object") {
                    obj[property] = this[property].deepExtend();

                }
                else{
                    Object.defineProperty(
                        obj, 
                        property, 
                        Object.getOwnPropertyDescriptor(this, property)
                    );
                }
            }

        }    
        return obj;
    }
}

var cpo = o.deepExtend();

o.c.d = {get t() {return 456}};
console.log(cpo);     //{a : 1, b : 2,c : {d : {get t() {return 123}}}}
console.log(o);       //{a : 1, b : 2,c : {d : {get t() {return 456}}}}

不可遍历属性对象的拷贝

例如拷贝Object.prototype

if(typeof Object.prototype.allCopy != "function") {
    Object.prototype.allCopy = function () {
        //获得共同的原型,target与this是兄弟关系
        var target = Object.create(Object.getPrototypeOf(this));
        var property = Object.getOwnPropertyNames(this);

        property.forEach(function (elem) {
            Object.defineProperty(
                target,
                elem,
                Object.getOwnPropertyDescriptor(this, elem)
                )
        },this);
        return target;
    }
}
console.log(Object.prototype.allCopy());
console.log(Object.allCopy().prototype); //说明也继承了原型,同级别
//{allCopy: ƒ, constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, …}
//{allCopy: ƒ, constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, …}

简洁化版

if(typeof Object.prototype.allCopy != "function") {
    Object.prototype.allCopy = function () {
        var target = Object.create(Object.getPrototypeOf(this), Object.getOwnPropertyDescriptors(this));
        return target;
    }
}
console.log(Object.prototype.allCopy());
console.log(Object.allCopy().prototype);
//{allCopy: ƒ, constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, …}
//{allCopy: ƒ, constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, …}

Infinity
293 声望9 粉丝

学前端中,只想激情优雅的写代码