2017-10-21 36 views
4

我想了解对象和Object.prototype之间的区别。因为要创建一个空对象,所以使用Object.prototype。我感觉为什么不是对象。javascript什么是对象与Object.prototype当在Object.create的原始参数中使用

我正在用以下方法创建一个对象。

方法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); 
+0

因为'新Object'等同于'的Object.create(Object.prototype中)'。这就是它的工作原理 - 初始化实例的构造函数与实例继承的对象不同。 – Bergi

回答

1

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

Object instanceof Function 

所以,你也可以这样做:

const o = new Object(); 

如果您已经阅读更多继承javascript,你知道调用一个函数new a ctually建立起从构造函数.prototype财产继承的对象,然后调用与构造是那个对象,所以上线等于:

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

如果你

Object.create(Object) 

你将创建一个继承构造函数的对象。我承认它很困惑的是对象实际上是一个功能,因此从Function.prototype的Object.prototype中继承继承...

+0

'对象实际上是一个函数,因此继承自继承自Object.prototype'的Function.prototype - 两个对象在这个句子中都是相同的。另外我试过'Function instanceof Object'它说的是真的。 –

+0

@sanotsh yes cause * Function *是一个*函数*,它继承了* Function.prototype *,它继承* Object.prototype * –

相关问题