【问题标题】:Difference between Object.create(prototype) and Object(prototype)Object.create(prototype) 和 Object(prototype) 的区别
【发布时间】:2015-04-24 11:49:08
【问题描述】:

一个快速但很难用谷歌搜索的问题:

var child = Object.create(parent.prototype);

var child = Object(parent.prototype); 

它们是相同的吗?


编辑:

这两个用于实现寄生组合继承模式的 inheritPrototype 函数示例提出了我的问题。

function inheritPrototype(childObject, parentObject) {
    var copyOfParent = Object.create(parentObject.prototype);
    copyOfParent.constructor = childObject;
    childObject.prototype = copyOfParent;
}

http://javascriptissexy.com/oop-in-javascript-what-you-need-to-know/

function inheritPrototype(subType, superType){
    var prototype = object(superType.prototype); 
    prototype.constructor = subType; 
    subType.prototype = prototype;
}

"Parasitic Combination Inheritance" in Professional JavaScript for Web Developers

【问题讨论】:

  • 在 Chromium 中 object 未定义。
  • object 应该是Object。
  • 已经回答了一点,谢谢!小“对象”是引号而不是拼写错误-我猜在这种情况下是引号拼写错误;)

标签: javascript oop object prototypal-inheritance


【解决方案1】:

不,它们不相同,见下文:

  1. Object.create(prototype) 将创建一个新的原型,它继承作为参数传递的prototype

  2. Object(obj) 或 new Object(obj) 将创建 obj 的新实例,同时调用构造函数。

现在,由于原型也是 javascript 对象,因此差异可能很小。但是Object.create 可以处理可能需要参数的构造函数,而new Object() 不会

this SO related answer 中的更多信息(例如差异继承、自定义属性等)

参考文献:

  1. Object.create on MDN
  2. new Object on MDN

更新 OP 编辑​​后

在我的 OOP javascript 框架 Classy.js 中,Object.create 周围有这个针对旧版浏览器的 polyfill,它可能会阐明您在差异之间的进一步问题:

   Create = Obj.create || function( proto, properties ) {
        var Type = function () {}, TypeObject;
        Type[PROTO] = proto;
        TypeObject = new Type( );
        TypeObject[__PROTO__] = proto;
        if ( 'object' === typeOf(properties) ) defineProperties(TypeObject, properties);
        return TypeObject;
    }

这个 polyfill 可以像 Object.create 一样处理任意构造函数(带参数),并确保 __proto__ 被正确分配。使用Object(prototype) 的模式是试图(弱)引用prototype。

【讨论】:

  • @hwechselberg,它与Object(obj) 相同,因为任何原型也是一个(javascript)对象。 Object 将调用构造函数,这可能对具有参数的构造函数(或复杂的构造函数)有问题,这与 Object.create 不同。也有其他差异(根据引用的链接)\。另见Object.create on MDN
  • @hwechselberg,在 OOP 库的 Object.create 周围使用 polyfill 更新了答案,以便更清楚地看到差异
【解决方案2】:

不,它们不一样:

Object(X) 或 new Object(X) 与 Object.create(X.prototype)

相同

Object(X) 将创建对象并运行Constructor(即新创建的对象继承自构造函数的原型)。 Object.create(X.prototype) 正在创建另外运行 Constructor 的对象。

Object(X) 或 new Object(X) 不像 Object.create()

Object.create() 在不运行 Constructor 的情况下创建对象(即,它将创建一个不从任何东西继承的对象)

【讨论】:

    【解决方案3】:

    New Object 将子对象设置为父对象:

    var parent = {name : 'proto'};
    var child = new Object(parent);
    console.log(child === parent);//=true
    //if I set child.name it will change parent.name
    

    Object create 返回一个对象实例,它使用第一个参数作为它的原型(原型链中使用的第一个原型)。

    child = Object.create(parent);
    console.log(child === parent);//=false
    //I can set child.name because it will be shadowed
    //  and will not change parent.name
    console.log(child.hasOwnProperty('name'));//=false
    

    更多关于原型的信息可以在这里找到:https://stackoverflow.com/a/16063711/1641941

    【讨论】:

    • 谢谢。但在那些例子中它是 Object.create(parent.**prototype**) 和 new Object(parent.**prototype**) - 所以仍然没有完全看到低谷
    • @hwechselberg 抱歉,没看到。但是,当您执行 child.prototype = new Object(parent.prototype) 然后 child.prototype === parent.prototype 时,为什么要将 child.prototype 设置为 parent.prototype 您不希望继承,因为您想要 Child。原型是一个(空)对象,其原型设置为您要继承的内容(在我的答案中的链接中解释)您想延长原型链(Child.prototype => Parent.prototype => Object.prototype)不是用 Parent.prototype 替换 Child.prototype: (Child.prototype === Patent.prototype => Object.prototype)
    猜你喜欢
    • 2019-11-29
    • 1970-01-01
    • 2016-01-08
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 1970-01-01
    相关资源
    最近更新 更多