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);
Prototypes are a special type of object and exist as a property on function-objects. When we try to access a key on a function-object, JavaScript will look at its prototype property to see if it's there. If not it will go up the prototype-chain to try to find it.
prototype is a property of a Function object. It is the prototype of objects constructed by that function. __proto__ is an internal property of an object, pointing to its prototype.
Every object in JavaScript has a built-in property, which is called its prototype. The prototype is itself an object, so the prototype will have its own prototype, making what's called a prototype chain. The chain ends when we reach a prototype that has null for its own prototype.
The prototype is an object that is associated with every functions and objects by default in JavaScript, where function's prototype property is accessible and modifiable and object's prototype property (aka attribute) is not visible. Every function includes prototype object by default.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With