繁体   English   中英

javascript在Object.create的proto参数中使用时,Object vs Object.prototype是什么

[英]javascript what is Object vs Object.prototype when used in Object.create's proto parameter

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

我通过以下方式创建对象。

方法1:

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__()

VS

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);

原因Object是用于构建对象的函数:

Object instanceof Function

所以你也可以这样做:

const o = new Object();

如果您已经在javascript中阅读了有关继承的更多信息,您知道使用new实现调用函数会构建一个继承自构造函数.prototype属性的对象,然后使用对象调用构造函数,因此上面的行等于:

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

如果你这样做

Object.create( Object )

您将创建一个继承构造函数的对象。 而且我承认, 对象实际上是一个函数非常混乱,因此从继承自Object.prototype的 Function.prototype继承...

使用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行不需要。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM