-
Notifications
You must be signed in to change notification settings - Fork 0
prototypes
Jorge Estanislao Barsoba edited this page Oct 15, 2021
·
1 revision
Mechanism by which objects in JavaScript inherit features from one another. That's why JavaScript is described as a prototype-based laguage.
The object and its prototype are connected through the __proto__ property.
All objects in JavaScript are instances of Object (except when deliberately prevented).
- One of the JavaScript's data types
- Stores keyed collections
- An object's prototype object may also have a prototype object, which it inherits features from, and so on
- Changes to Object.prototype are seen by all objects (except when the properties or methods are overriden)
- Methods and properties are accessed by walking up the chain
Constructor function def:
function Person(first, last) {
this.name = {
'first': first,
'last': last
};
}Instance creation:
let person1 = new Person('Bob', 'Smith');person1 Inherits from prototype > Person Inherits from prototype > Object
If we call person.valueOf():
- The engine checks if
person1has avalueOfmethod - As it does not, it checks if
Personhas it - As it does not, it checks if
Objecthas it, and as it does, the method is called!
The
prototypeproperty is basically a bucket for storing properties and methods that we want to be inherited by objects further down the prototype chain
Sources