28
loading...
This website collects cookies to deliver better user experience
tl;dr: Understanding Prototypal inheritance, uses & specifics. Composed with code, console & explanatory approach.
Problems are understood crystal clear if seen from a right perspective.
Moving on the same axis: consider a biological perspective for Inheritance concept in Javascript. Let Mike be the father👨 and Joe be his son 👦. As per biology, Joe would inherit some "properties" or an "image" of his father, Mike, unknowingly. This image or "properties" or say versions of the original copy inherited from parent object to child object refers to "Prototypal Inheritance". console.log(Array.prototype); //brings all basic properties and methods associated with an Array Object and appends it to Array.prototype.
function Mike () {
this.name = 'Mike';
this.skill = 'JavaScript';
}
//Mike inherits an Object prototype. The "prototype" gets created with function Mike declaration, automatically.
Mike.hasOwnProperty('prototype');
console.log(Mike.prototype);
//paste above code on console.
The hasOwnProperty() method returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it)~ MDN
Syntax: function_name.prototype.property_name = property_value;
Mike.prototype.putName = function () {
return 'My name is ' + this.name;
}
console.log(Mike.prototype); //putName method added to Mike prototype
var Joe = new Mike();
console.log(Joe); //Son(joe) inherits father prototypal properties
var Joe = new Mike();
var Joe = new Object();
Joe.[[Prototype]] = Mike.prototype;
Mike.call(Joe);
In layman terms, proto acts as a wormhole that references properties from one Object to another Object, during prototypal chain lookup mechanism.
Conside the Prototypal chain built using above code:
// Joe ---> Mike ---> Object.prototype ---> null
Object.proptotype is supported by various browsers and is very fast. It's also Just-in-time(JIT) Compilation optimizable in nature.
Prototypal Inheritance enables all the properties you define in the parent prototype to be effectively shared by all instances. Changes in the Parent instance properties will be mitigated automatically to all Child Instances.
Null has no prototype, thus ends the prototypal chain. [ P.S: typeof Null= object]
A function in JavaScript always have a default "prototype" property — with one exception: an Arrow function. Arrow function doesn't have a default prototype property.
Making prototypal Code performant. If code is performance constrained, look up time for properties in prototypal chain can have negative effects. As deeper you go in the scope chain, the more computational overheads occurs as [[Prototype]] is looked recursively. To solve the lookup problem, an inbuilt property called as hasOwnProperty can be used. It deals with an Object's own properties, returns a boolean after search and does not traverses whole prototypal chain. As a developer, be aware of the length of your prototypal chains and break them up if necessary to avoid possible performance problems.
Don't extend Object.prototype for adding non-native properties. It breaks encapsulation, and creates a "Monkey Patching" pattern.
Initializations of constructors on parent object might put overload of unwanted methods in child. Be careful while doing it.