Welcome All,
Till our Last Post we've seen only pretty basics in Javascript.From now we are going to Jump into little deep about Objects in Javascript,as i've told you that Javascript Object are very Deep and He is very Strong one,as most of the things in javascript is an Object.
Let's Quick Dive into it,in this Post we are Going to know about Prototype in Javascript.
Prototype
As in many Programming Languages,they have Inheritance to access the Feature from his Parent class or Object.As like that Javascript can inherit features from one Object to another Object via Prototype.Unlike in other Programming languages like Java,C++ they have Classical Inheritance but in Javascript it is Prototypal Inheritance.
Every Object has it's own Property called Prototype.And Prototype itself is an another Object and it contains another prototype and it is somewhat called as the Prototype Chain.This Chain ends when it's Prototype is null.
Suppose you have an object person with a property called name:
`let person = {'name' : 'John'}`
When examining the person object in the console, youโll find that the person object has a property called prototype denoted by the [[Prototype]]
The prototype itself is an object with its own properties:
Suppose if you want to access a property from an Object it'll return the value if exist,or else if the Property is not present in the Object the Javascript engine will search for the Property in the Object Prototype and even if it can't find it there it'll search it in the Prototype's Prototype untill it finds the Property or reaches the end of the prototype Chain.
For example, you can call the toString() method of the person object like this:
The toString() method returns the string representation of the person object. By default, itโs [object Object] which is not obvious.
In this example on calling the toString() method,the Javascript engine will search for it in the Person Object and it could not find it there and it continue to search for it in the Object Prototype and it will find it there.
Since the personโs prototype has the toString() method, JavaScript calls the toString() of the personโs prototype object
for now that's it,we'll see more intresting things about the Prototype and it's inheritance in upcoming post.
Many Thanks for Your Time,
Sam
Top comments (5)
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
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
I couldn't get through the first few lines of this article. Basic grammar and punctuation are needed when writing an article
I'm so much bad in English. Getting sort everyday !.
Suggest me tip if you could ,to rectify the grammar problem.
I'd strongly suggest writing your article in a proper text editor like Microsoft Word or OpenOffice. It can suggest fixes for grammar, punctuation, and spelling mistakes.
Also these days you can ask AIs like ChatGPT to correct it or even translate it from your native language.
Good luck learning and I'm sure you're getting better every day, but there is definitely something you can do now to improve readability of your articles.