I am trying to understand the difference between Object and Object.prototype. Because to create an empty object Object.prototype is used. I felt why not Object.
I am creating an object in the following ways.
Method 1:
o = Object.create(Object.prototype,{ p : {value: "test"} });
console.log(o.__proto__);
result is:
Object {__defineGetter__: function, __defineSetter__: function, hasOwnProperty: function, __lookupGetter__: function, __lookupSetter__: function…}
and
console.log(o)
result is
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__);
result is:
function Object() { [native code] }
and:
console.log(o)
result is:
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>
In general i found that:
o = {};
// is equivalent to:
o = Object.create(Object.prototype);
why not
o = {};
// is equivalent to:
o = Object.create(Object);
Cause Object
is a function used to build objects:
Object instanceof Function
So you can also do:
const o = new Object();
If you have read more about inheritance in javascript, you know that calling a function with new
actually builds up an object that inherits from the constructor functions .prototype
property and then calls the constructor with this being that object, so the upper line equals:
const o = Object.create( Object.prototype );
Object.call( o );
If you do
Object.create( Object )
you will create an object that inherits the constructor function. And i admit that its quite confusing that Object is actually a function , that inherits therefore from Function.prototype that inherits from Object.prototype ...
Using code from 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)
}
if .prototype
is removed in the first line, the the 'object'.constructor is not created, so nothing gets inherited with new
. Not needed in 2nd line because of 1st.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.