如何在不调用构造函数的情况下复制对象及其原型链?

时间:2011-12-19 19:17:27

标签: javascript copy coffeescript prototype-oriented

如何在不调用构造函数的情况下复制对象及其原型链?

换句话说,在下面的例子中,函数dup会是什么样子?

class Animal
  @sleep: -> console.log('sleep')
  wake: -> console.log('wake')
end
class Cat extends Animal
  constructor: ->
    super
    console.log('create')

  attack: ->
    console.log('attack')
end

cat = new Cat()         #> create
cat.constructor.sleep() #> sleep
cat.wake()              #> wake
cat.attack()            #> attack

dup = (obj) ->
  # what magic would give me an effective copy without
  # calling the Cat constructor function again.

cat2 = dup(cat)          #> nothing is printed!
cat2.constructor.sleep() #> sleep
cat2.wake()              #> wake
cat2.attack()            #> attack

尽管看起来很痛苦,但这是一个jsfiddle的例子。

尽管我的例子中只使用了函数,但我还需要这些属性。

2 个答案:

答案 0 :(得分:5)

function dup(o) {
    return Object.create(
        Object.getPrototypeOf(o),
        Object.getOwnPropertyDescriptors(o)
    );
}

这取决于ES6 Object.getOwnPropertyDescriptors。你可以效仿它。 Taken from pd

function getOwnPropertyDescriptors(object) {
    var keys = Object.getOwnPropertyNames(object),
        returnObj = {};

    keys.forEach(getPropertyDescriptor);

    return returnObj;

    function getPropertyDescriptor(key) {
        var pd = Object.getOwnPropertyDescriptor(object, key);
        returnObj[key] = pd;
    }
}
Object.getOwnPropertyDescriptors = getOwnPropertyDescriptors;

Live Example

将此转换为coffeescript会留给用户练习。另请注意,dup浅拷贝拥有属性。

答案 1 :(得分:0)

您应该使用特殊的__proto__成员,该成员在每个对象中都可用,并且是指向对象类的原型的指针。以下代码使用纯javascript:

function dup(o)
{
    var c = {};

    for (var p in o)
    {
        c[p] = o[p];
    }
    c.__proto__ =  o.__proto__;

    return c;
}
相关问题