10
I was reading this article in the English OS and came across the term Differential inheritance.
Which, exactly, is differential inheritance?
It is possible to have a minimum example, preferably in Javascript?
10
I was reading this article in the English OS and came across the term Differential inheritance.
Which, exactly, is differential inheritance?
It is possible to have a minimum example, preferably in Javascript?
8
Free translation of wikipedia article (so thin that we even suspect):
Differential inheritance is a common inheritance model used in prototype-based programming languages such as Javascript, Io, and Newtonscript. It operates on the principle that many objects derive from other more generic objects, and only differ from them in a few details; generally the derived object maintains a list of pointers internally for more generic ones.
In this question from the OS, say that differential inheritance is a synonym of prototypical inheritance, but that its use in relation to Javascript is to emphasize the "pure" pattern in which it is used Object.create
to inherit directly from another object instead of the new
, which is a device that Javascript has to look more like class-based languages (by the way, in ES6 we will have until the keyword class
, but internally everything will continue to work the same way, the inheritance will continue prototypic).
8
Complementing the bfavaretto response.
In languages like Java and . NET, instances are all bastard**, because only types are entitled to inheritance.
In Javascript, everyone can "inherit" from everyone. Every Javascript object has a prototype. The prototype tells an object what properties it possesses via inheritance. And the language allows you to do very legal atrocities like this:
var foo = {}; // esse objeto herda de Object.
foo.prototype = Array.prototype; // E agora ele tem um protótipo comum com o Array :D
You can take it a step further.
var bar = {nome: "John Doe", idade: 21};
foo.prototype = bar; //sim, isso é permitido.
You can take it further...
var bar = {nome: "John Doe", idade: 21};
var foo = function () {};
foo.prototype = bar;
var ni = new foo(); // sério, veja as propriedades de ni.
We have an adoption process here, because now the "father" of foo
becomes bar
. The father of the object becomes another object, not a type.
Yes, these modern family relationships, with such tenuous ties, are really quite complex...
Seriously, this story of inheriting from an object and not from a type is called differential inheritance. This gives a lot of power to the programmer - you can define inheritances dynamically and at runtime, and with more flexibility than in the "traditional OO model". But these powers carry great responsibilities, especially in a world where the only safe typing is duck typing.
** That is NOT a technical term ;)
Browser other questions tagged javascript oop inheritance
You are not signed in. Login or sign up in order to post.