Open allenGKC opened 4 years ago
So where is the end of the prototype chain.
So where is the end of the prototype chain.
@mdAliMaaz [What is the end of prototype chain in JavaScript -- null or Object.prototype? This may helps u
Hi, great work first of all. I can see there's 9 chapters altogether and wonder if there would be more to come. Thanks :)
Hi, great work first of all. I can see there's 9 chapters altogether and wonder if there would be more to come. Thanks :)
Hi, great work first of all. I can see there's 9 chapters altogether and wonder if there would be more to come. Thanks :)
Hi @jacinyan I check the Jast Javascript and there is no more articles
09. Prototypes
Prototypes
According to the rules we’ve learned, if we read it, we get undefined:
But we can modify the codes like this:
Prototypes in Action
Thanks to that
__proto__
: human line, the answer is different now:The Prototype Chain
A prototype isn’t a special “thing” in JavaScript. A prototype is more like a relationship. An object may point at another object as its prototype.
This sequence of objects to “visit” is known as our object’s prototype chain.
Shadowing
In other words, once we find our property, we stop the search.
Assignment
Consider this example:
Before the assignment, both expressions result in 32:
Then we need to execute this assignment:
So
gwen.teeth = 31
creates a new own property called teeth on the object that gwen points at. It doesn’t have any effect on the prototype:The result is:
When we read a property that doesn’t exist on our object, then we’ll keep looking for it on the prototype chain. If we don’t find it, we get undefined. But when we write a property that doesn’t exist on our object, that will create that property on our object. Generally saying, prototypes will not play a role.
The Object Prototype
Surprisingly,
obj.__proto__
is not null or undefined! Instead, you’ll see a curious object with a bunch of properties, including hasOwnProperty.We’re going to call that special object the Object Prototype:
At first, this might be a bit mindblowing. Let that sink in. All this time we were thinking that {} creates an “empty” object. But it’s not so empty, after all! It has a hidden
__proto__
wire that points at the Object Prototype by default.These “built-in” properties are nothing more than normal properties that exist on the Object Prototype. Our object’s prototype is the Object Prototype, which is why we can access them. (Their implementations are inside the JS engine.)
An Object with No Prototype
This will produce an object that truly doesn’t have a prototype, at all. As a result, it doesn’t even have built-in object methods:
Polluting the Prototype
We mutated the Object Prototype by adding a smell property to it. As a result, both detectives now appear to be using a banana-flavored perfume:
Mutating a shared prototype like we just did is called prototype pollution.
Recap
obj.__proto__.prop
, then it will look forobj.__proto__.__proto__.prop
, and so on, until it either finds our property or reaches the end of the prototype chain.