What is “prototype ancestry” in the above definition? The Object has a prototype but that prototype object can also have a prototype and so on. We call it the prototype chain. So the object can use any properties and methods that are on that prototype chain. That’s why I used prototype ancestry to the above definition.
Define an Object ProtoType – You can define an object’s prototype and that’s where the power prototype comes in.
02. Understand prototype with diagrams
Employeeobject in the following diagram and it is identified by Emp1. It has some properties and values such as
fullAddressand a default property. This default property is a link to another object.
If follow the link of the default property assigned to Emp1 object, we have another object that can have properties and methods assigned to it as well. It can also have the link to another object. We call this as the protoType Chain. You can continue the prototype chain until you get to the topmost object.
What if we refer
country. If it can’t find, it would follow the prototype link to next object that is linked and look for the property of
country at there. It it doesn’t find it, continues up the prototype chain. If it gets to the uppermost object in the prototype chain and not able to find the property that is referred to, then it returns undefined.
03. Understand prototype with Examples
What happens if we create our own toString() method in
Emp1 object? I create and set the custom function to the toString attribute in Emp1 object as follow,
Now let see where toString() method is located in the prototype chain,
Binary Search Algorithm with Example | Data structuresG, 0 Comments Binary search will take less time than the linear search. The one pre-request of binary search- Array should be sorted. If the data is not sorted in the array, you cannot apply a binary...
React Interview Questions with Answers- (2020)G, 0 Comments Follow me on Medium In this article, we're going to talk all react interview questions with answers and will be useful for the people who get readying or for the interviews or want to improve the...