Prototypes in JavaScript:
- Overview: Prototypes are a fundamental concept in JavaScript’s object-oriented programming model. They provide a way to inherit properties and methods from an object (the prototype) to other objects (instances).
- Inheritance Mechanism: When you create an object using a constructor function like
Foo, JavaScript creates a new instance and links it to the constructor’sprototypeproperty. This link allows the instance to access and inherit properties and methods defined on theprototype.
Let’s breakdown of the Code below:
function Foo(params1) {
this.params1 = params1;
}
Function Foo(params1): This line defines a constructor function namedFoothat takes a single parameterparams1.this.params1 = params1;: Inside the constructor, thethiskeyword refers to the newly created object instance. This line assigns the value ofparams1to a property namedparams1on the instance.
Prototype Inheritance:
- Every function in JavaScript has a
prototypeproperty, initially set to an empty object. - When you create an instance of
Foo, JavaScript does the following:- Creates a new empty object.
- Sets the object’s
__proto__property (hidden internal property) to theprototypeof theFoofunction (which is initially an empty object). - Executes the
Foofunction’s code, passing the new object asthis. This allows you to define properties likeparams1on the instance.
Accessing Inherited Properties:
- If an instance (like
const foo = new Foo("value")) tries to access a property it doesn’t have (e.g.,foo.nonexistent), JavaScript automatically looks for it on the instance’s__proto__(theFoo.prototypeobject). - If the property is found on the
prototype, it’s inherited and made available to the instance.
All Javascript Object inherit properties and methods from prototype.
Let’s dive in for real world, firstly Create Object Constructor
function Foo(params1) {
this.params1 = params1;
}
Call property with new instance
const instance = new Foo("Params 1");
console.log(instance.params1);
// ---- output: Params 1
We can not add new property to existing Object Constructor
Foo.newProperty = "This is new Property";
console.log(instance.newProperty);
// ---- output: undefined
And last we can add new property to Object Constructor
Foo.prototype.bar = "This is new Bar";
console.log(instance.bar);
// ---- output: This is new Bar
For complete codes
function Foo(params1) {
this.params1 = params1;
}
/**
* Create new instance as representative of Foo
* all Foo's stuff will be inherits to new instance
*/
const instance = new Foo("Params 1");
// take property default
console.log(instance.params1);
// ---- output: Params 1
// can not add new property
Foo.newProperty = "This is new Property";
console.log(instance.newProperty);
// ---- output: undefined
// add new property
Foo.prototype.bar = "This is new Bar";
console.log(instance.bar);
// ---- output: This is new Bar
Key Points:
- Prototypes provide a simple mechanism for inheritance in JavaScript.
- Instance properties take precedence over inherited properties from the prototype.
- You can modify the
prototypeproperty of a constructor function to add shared properties and methods for all instances.
Thank you for reading this article, I hope you find it useful. Happy coding! 🔥