+ 3
What is prototypal inheritance pattern
What it's mean. Anybody help me.
6 Answers
+ 6
In JavaScript, objects are prototype based. Here is my understanding of prototypes.
Every object holds a link to a prototype object that contains the properties of its base object. e.g a function's prototype refers to Function.prototype.
Prototypical inheritance is when objects inherit from another object's prototype. For example, a Function is an Object because Function.prototype is an extension of Object.prototype. All properties of objects are inherited in Function.prototype.
Prototypical inheritance can easily be achieved by using the Object.create function.
var obj = {
name: 'obj'
};
var obj2 = Object.create(obj);
/* or the deprecated option
var obj2 = {
__proto__: obj
};*/
console.log(obj2.name)
https://www.educative.io/blog/understanding-and-using-prototypal-inheritance-in-javascript
To understand how changes to objects are cascaded through prototypes. See this by David Carroll
https://code.sololearn.com/WhmOwy6M1c1z
+ 7
Ore Thanks for the mention. That link you shared is well written. Thanks for sharing that as well. đ
I strongly recommend reading this link from start to finish a couple of times.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain
Afterwards, revisit your current explanation in this post and see if you can spot the various statements that should be refined. đ
You're definitely grasping the general concept. But there are a few statements that reveal a few gaps to close.
I could point out the subtle corrections for you..., but you're so close, I think this self discovery will help solidify your understanding. đ
+ 4
Kiwwi# Sort of, but not quite.
With class based inheritance in C#, System.Object is the top most base class of all .NET types - which is defined at the class level before object creation at runtime.
Prototypal inheritance, on the other hand, involves a chain of referenced object instances that all point to the same shared root prototype object instance.
So... the difference is one implements inheritance at the class level, before it can be used to create an object instance. The other implements inheritance when the object instance is created at runtime.
Hopefully this makes sense.
+ 3
David Carroll Thanks for the link. It cleared up a lot of misconceptions.
Allamprabhu Hiremath Here is additional explanation.
Every object 'holds a link' to a prototype object(usually called __proto__) which is by default the prototype of its parent object in the prototypical chain.
Inheriti built-in prototype has been made easy by using the language constructs.
e.g
var arr= [4, 6];
// arr inherits Array prototype object)
// all methods in Array.prototype like indexOf can be called on arr
For non built-ins you can use the `new` keyword.
function Person () {
this.name = 'Ore'; // A own property
}
// A prototype property.
Person.prototype.eat = function() {
console.log('Yum!');
}
var p = new Person();
// p has inherited the Person prototype
The prototype chain is defined thus:
p -> Person -> Function -> Object -> null
Since ES 6, you can use class keyword to achieve this.
class Person {
constructor() {
this.name = 'Ore';
}
eat() {
console.log('Yummy');
}
}
Hope this explains it well
+ 2
David Carroll think it makes sense, but design patterns always need a strong theory background to catch it 100%
Agnostic programming will help there
+ 1
David Carroll so in C# System.Object the same, right?