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);
new Object
is equivalent toObject.create(Object.prototype)
. That's how it works - the constructor function that initialises an instance is not the same thing as the object that the instances inherit from. – Bergi Oct 21 '17 at 15:40