![](/img/trans.png)
[英]Object.create(Object.prototype) vs var task=Object.create(Object)
[英]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.