Every Object has it's own Property called Prototype.And Prototype itself is an another Object...
This isn't entirely true, it is possible to make an object without a prototype - by taking advantage of what you said - 'This Chain ends when it's Prototype is null' - we can actually start the chain with null:
// normal objectconstobj1={}obj1.b=3console.log(typeofobj1)// object - as expectedconsole.log(Object.getPrototypeOf(obj1))// Object { ... } - normal object prototypeconsole.log(obj1.__proto__)// Object { ... }console.log(obj1.b)// 3 - object properties work normally// prototype-less objectconstobj2=Object.create(null)obj2.b=3console.log(typeofobj2)// object - as expectedconsole.log(Object.getPrototypeOf(obj2))// nullconsole.log(obj2.__proto__)// undefined - nothing here! (as our object lacks the getter for __proto__)console.log(obj2.b)// 3 - object properties work normally
So, the object created when you use the object literal {} is actually equivalent to Object.create(Object.prototype).
Creating an object without a prototype in this manner is quite unusual, but does have some use cases. One that springs to mind is creating a very pure dictionary where there is no risk at all of property name collisions from the prototype chain - useful when you have no direct control over the property names being used, or need to use property names that already exist on the prototype. Saying that, if you find yourself in this position you may want to consider using a Map object instead as they are generally faster and consume - IIRC - less memory.
You really taught me something, as a novice i will get bettter everyday.
Thanks for your corrections and keep supporting me by reading me posts.
Grateful Time! Brother
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
This isn't entirely true, it is possible to make an object without a prototype - by taking advantage of what you said - 'This Chain ends when it's Prototype is null' - we can actually start the chain with
null:So, the object created when you use the object literal
{}is actually equivalent toObject.create(Object.prototype).Creating an object without a prototype in this manner is quite unusual, but does have some use cases. One that springs to mind is creating a very pure dictionary where there is no risk at all of property name collisions from the prototype chain - useful when you have no direct control over the property names being used, or need to use property names that already exist on the prototype. Saying that, if you find yourself in this position you may want to consider using a
Mapobject instead as they are generally faster and consume - IIRC - less memory.You really taught me something, as a novice i will get bettter everyday.
Thanks for your corrections and keep supporting me by reading me posts.
Grateful Time! Brother