【问题标题】:javascript what is Object vs Object.prototype when used in Object.create's proto parameterjavascript 在 Object.create 的 proto 参数中使用什么是 Object vs Object.prototype
【发布时间】:2018-04-02 10:52:41
【问题描述】:

我试图了解 Object 和 Object.prototype 之间的区别。因为要创建一个空对象 Object.prototype 被使用。我觉得为什么不是 Object。

我正在通过以下方式创建一个对象。

方法一:

o = Object.create(Object.prototype,{ p : {value: "test"} });
console.log(o.__proto__);

结果是:

Object {__defineGetter__: function, __defineSetter__: function, hasOwnProperty: function, __lookupGetter__: function, __lookupSetter__: function…}

console.log(o)

结果是

Object {p: "test"}
    p : "test"
    __proto__ : Object
        constructor : function Object()
        hasOwnProperty : function hasOwnProperty()
        isPrototypeOf : function isPrototypeOf()
        propertyIsEnumerable : function propertyIsEnumerable()
        toLocaleString : function toLocaleString()
        toString : function toString()
        valueOf : function valueOf()
        __defineGetter__ : function __defineGetter__()
        __defineSetter__ : function __defineSetter__()
        __lookupGetter__ : function __lookupGetter__()
        __lookupSetter__ : function __lookupSetter__()
        get __proto__ : function __proto__()
        set __proto__ : function __proto__()

o = Object.create(Object,{ p : {value: "test"} });
console.log(o.__proto__);

结果是:

function Object() { [native code] }

和:

console.log(o)

结果是:

Function {p: "test"}
    p : "test"
    __proto__ : function Object()
        arguments : null
        assign : function assign()
        caller : null
        create : function create()
        defineProperties : function defineProperties()
        defineProperty : function defineProperty()
        entries : function entries()
        freeze : function freeze()
        getOwnPropertyDescriptor : function getOwnPropertyDescriptor()
        getOwnPropertyDescriptors : function getOwnPropertyDescriptors()
        getOwnPropertyNames : function getOwnPropertyNames()
        getOwnPropertySymbols : function getOwnPropertySymbols()
        getPrototypeOf : function getPrototypeOf()
        is : function is()
        isExtensible : function isExtensible()
        isFrozen : function isFrozen()
        isSealed : function isSealed()
        keys : function keys()
        length : 1
        name : "Object"
        preventExtensions : function preventExtensions()
        prototype : Object
        seal : function seal()
        setPrototypeOf : function setPrototypeOf()
        values : function values()
        __proto__ : function ()
        [[FunctionLocation]] : <unknown>

总的来说,我发现:

o = {};
// is equivalent to:
o = Object.create(Object.prototype);

为什么不

o = {};
// is equivalent to:
o = Object.create(Object);

【问题讨论】:

  • 因为new Object 等价于Object.create(Object.prototype)。这就是它的工作原理——初始化实例的构造函数与实例继承的对象不同。

标签: javascript javascript-objects


【解决方案1】:

因为Object是一个用于构建对象的函数:

Object instanceof Function

所以你也可以这样做:

const o = new Object();

如果你已经阅读了更多关于 javascript 继承的内容,你就会知道使用new 调用函数实际上会构建一个从构造函数.prototype 属性继承的对象,然后使用 this 调用构造函数em> 是那个对象,所以上面一行等于:

const o = Object.create( Object.prototype );
Object.call( o );

如果你这样做了

Object.create( Object )

您将创建一个继承构造函数的对象。而且我承认,Object 实际上是一个 function,它继承自 Function.prototype,而 Function.prototype 继承自 Object。原型 ...

【讨论】:

  • Object is actually a function, that inherits therefore from Function.prototype that inherits from Object.prototype - 这句话中的两个对象是否相同。我也试过Function instanceof Object它说是真的。
  • @sanotsh 是的,因为 Function 是一个 function,它继承了 Function.prototype,它继承了 Object.prototype
【解决方案2】:

使用来自 js-calendar.js 的代码:

function defineProperties(target, props)
{
  '''
  Object.defineProperty(target, descriptor.key, descriptor);
}
return function(Constructor,protoProps, staticProps)
{
   if (protoProps) defineProperties(Constructor.prototype,protoProps)
   if (staticProps) defineProperties(Constructor,protoProps)
}

如果.prototype 在第一行被删除,'object'.constructor 不会被创建,所以new 不会继承任何东西。由于第 1 行,第 2 行不需要。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-13
    • 2014-12-07
    • 2012-02-20
    • 2021-06-08
    • 1970-01-01
    • 2019-08-26
    相关资源
    最近更新 更多