I do understand that we can add properties to Constructor using prototype. But how come a object have prototype.
I do not understand it, I am totally confused. Can someone explain me please ?
how come a object have prototype?
There it’s my notes:
Instance object has proto attribute, called prototype, is also an object, this attribute is used by the browser, not a standard attribute
The constructor has a prototype property called a prototype, which is also an object. This property is for programmers. It is a standard property ------>prototype—> can be called a prototype object.
The __proto__ of the instance object is equal to the prototype in the constructor --->true
And because the instance object is created by the constructor, there is a prototype object prototype in the constructor.
The __proto__ of the instance object points to the prototype object prototype of the constructor
Yes, objects have a prototype. When you create an object, it inherets certain methods (functions attached to a prototype or class) and properties. For example
The method hasOwnProperty is part of the object prototype that gets inherited when we create our object. We didn’t need to create it, it was already there.
We added a method to the prototype and because we did it before our object was created, it inherited it.
The whole thing about prototypes and inheritence and stuff - it kind of gets glossed over in JS. This is part of object oriented programming. JS isn’t OOP, but it is OOPish. If you were doing a language like Java, this kind of understanding would be crucial.
Everything that isn’t a primitive data type will have a prototype. These would include Objects, Arrays, and Functions. Technically these are all Objects in JavaScript. An Array prototype inherits the Object prototype and adds some stuff onto it.
This is weird stuff. Don’t get frustrated if it doesn’t sink in the first time. Just try to understand it a little bit better each time. The book You Don’t Know JavaScript was mentioned. Those are great books. But be warned - they are hard books, especially the deeper you go. But they would be a good goal - if you understand everything in those books, you’ll know JS very well. But don’t get frustrated if they are difficult. They are free online in electronic form on their git repo.
It might be worth adding than in modern JavaScript it is very unusual to use Object prototypes in your own code. Nowadays, we can use the class keyword to define object ‘blueprints’ and use extend to inherit properties from another class.
Under the hood, classes still make use of JavaScript’s prototype chain, but they provide a much friendlier syntax which is more similar to OOP in other languages.
That’s true that we’re less likely to use this and there is more preference for classes, understanding this is still important and might come in handy if you want to manipulate methods or properties directly on JS types. (Not that you were contradicting that.)
I am confused as a part that we can add properties to constructors and their instances by prototype. Prototype is property of constructor. Prototype is a object, But objects also have prototype. After reading all this, I can’t figure it out.
When you add the prototype to the constructor, a newly created object gets the prototype as well in a special slot called __proto__. Yes, it’s incredibly confusing. There’s a painstaking explanation of it in the excellent book You Don’t Know Javascript