+ 3

What is prototypal inheritance pattern

What it's mean. Anybody help me.

9th Sep 2020, 2:23 PM
Allamprabhu Hiremath
6 Respuestas
+ 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
9th Sep 2020, 3:11 PM
Ore
Ore - avatar
+ 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. 😉
9th Sep 2020, 5:24 PM
David Carroll
David Carroll - avatar
+ 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.
10th Sep 2020, 7:47 AM
David Carroll
David Carroll - avatar
+ 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
9th Sep 2020, 6:34 PM
Ore
Ore - avatar
+ 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
10th Sep 2020, 8:08 AM
Kiwwi#
Kiwwi# - avatar
+ 1
David Carroll so in C# System.Object the same, right?
10th Sep 2020, 2:23 AM
Kiwwi#
Kiwwi# - avatar