Js Object Prototype
# JavaScript prototype (Prototype Object)
In JavaScript, the prototype is a very important concept that provides a mechanism for inheritance and sharing properties among objects. Every JavaScript object has an associated prototype object. Through the prototype object, properties and methods can be shared, thereby reducing memory usage.
All JavaScript objects inherit properties and methods from a prototype object.
* A **prototype** is an object that serves as a template or blueprint for other objects.
* When an object tries to access a property or method, if it is not found on the object itself, JavaScript will look up the prototype chain until it finds the corresponding property or method, or reaches the end of the chain, which is `null`.
* * *
## The __proto__ Property of Objects
Every JavaScript object (except `null`) automatically has a hidden property called `__proto__`, which points to the object's prototype. This `__proto__` is key to implementing inheritance:
let obj = {}; console.log(obj. __proto__ ); // Output: , meaning obj's prototype is Object.prototype
* * *
## Constructor Functions and Prototypes
In previous chapters, we learned how to use object constructors:
## Example
function Person(first, last, age, eyecolor){this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eyecolor; }var myFather = new Person("John", "Doe", 50, "blue"); var myMother = new Person("Sally", "Rally", 48, "green");
[Try it Β»](#)
We also know that new properties cannot be added to an object that already has a constructor:
## Example
Person.nationality = "English";
[Try it Β»](#)
To add a new property, it needs to be added within the constructor function:
## Example
function Person(first, last, age, eyecolor){this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eyecolor; this.nationality = "English"; }
[Try it Β»](#)
When you create an object using a constructor function, the `prototype` property of the constructor becomes the prototype for all instance objects created by that constructor.
## Example
function Person(name){
this.name= name;
}
Person.prototype.sayHello=function(){
console.log("Hello, my name is "+this.name);
};
let alice =new Person("Alice");
alice.sayHello();// Output: Hello, my name is Alice
In this example, `Person.prototype` is the prototype of the `alice` object, so `alice` can access the `sayHello` method.
* * *
## Prototype Chain
In JavaScript, objects inherit through the prototype chain. When an object tries to access a property or method, JavaScript first checks if the object itself has that property or method. If not, it looks up the prototype chain.
let obj = {}; console.log(obj.toString()); // Output: // This `toString` method is actually inherited from `Object.prototype`
In the example above, the `obj` object does not define a `toString` method, so JavaScript looks up the prototype chain and eventually finds the method in `Object.prototype`.
* * *
## Modifying the Prototype
You can dynamically modify
YouTip