我正在尝试理解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);
发布于 2017-10-21 12:40:05
原因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,这让人非常困惑……
发布于 2018-10-12 01:35:41
使用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”.structor,因此new
不会继承任何内容。在第二行中不需要,因为第一行。
https://stackoverflow.com/questions/46863128
复制相似问题