This answer on Object.create()
method in JavaScript in SO talks about differential inheritance. It goes on to say this :
This methods allows you to easily implement differential inheritance, where objects can directly inherit from other objects.
As far as I know JavaScript always allowed objects to directly inherit from other objects via prototypal inheritance. There is no concept of a class in JavaScript. So what does differential inheritance really mean and why is it called so?
P.S: I had dropped a ment on that answer some time back but I didn't receive any replies. So wanted to check with the larger and awesome munity of SO JavaScript users.
This answer on Object.create()
method in JavaScript in SO talks about differential inheritance. It goes on to say this :
This methods allows you to easily implement differential inheritance, where objects can directly inherit from other objects.
As far as I know JavaScript always allowed objects to directly inherit from other objects via prototypal inheritance. There is no concept of a class in JavaScript. So what does differential inheritance really mean and why is it called so?
P.S: I had dropped a ment on that answer some time back but I didn't receive any replies. So wanted to check with the larger and awesome munity of SO JavaScript users.
Share Improve this question edited May 23, 2017 at 12:23 CommunityBot 11 silver badge asked Jul 21, 2013 at 11:14 GeekGeek 27.2k45 gold badges157 silver badges234 bronze badges 5- 7 Apparently, "differential inheritance" is just another way of saying "prototypal inheritance". – rid Commented Jul 21, 2013 at 11:26
- 1 This may describe better. – The Alpha Commented Jul 21, 2013 at 13:58
- @SheikhHeera: Actually it doesn't, because the example given there does not work and is at least misleading. – Bergi Commented Jul 22, 2013 at 21:06
- @Bergi, Misleading ? Then what is the right-leading, any example of your own or reference ? – The Alpha Commented Jul 22, 2013 at 21:34
-
@SheikhHeera: Working on it :-) The
Object.prototype.clone
method should be a straightreturn Object.create(this)
, then the example at MDN makes more sense. Edit: Ah, this old version was better. – Bergi Commented Jul 22, 2013 at 22:02
1 Answer
Reset to default 11As the other menters and the articles they've linked already suggested, differential inheritance is "just" the normal, known prototypical inheritance.
Yet by using the term differential inheritance you focus on a more pure pattern than it is known in JavaScript (though quite mon in other prototypical languages like Self, NewtonScript or Io). Unlike the pseudo-classical pattern, there are no constructors with new
usage at all. Instead, by using Object.create
you create a new object inheriting from the target object in one step, and then you create the necessary instance properties (only those that are different) manually (not with a constructor). It's not unusual to inherit from an object that you would consider to be an instance otherwise, instead of from a dedicated prototype object.
var object = Object.prototype;
// a Person constructor and a Person.prototype method would be more familiar
var person = Object.create(object);
person.greet = function() {
console.log("Hi, I'm "+this.firstName+" "+this.lastName);
};
// as would new Person("John", "Doe");
var jo = Object.create(person);
jo.firstName = "John";
jo.lastName = "Doe";
// but what now? We stay with "simple" Object.create here
var ja = Object.create(jo);
ja.firstName = "Jane";
jo.greet();
ja.greet();
As you can see creating a Jane is simple, but we would have to break with the new Constructor()
pattern if we had used it. That's why some JS gurus are advocating to use the pure pattern everywhere (so that you understand better what's going on) and are happy to have been given Object.create
with EcmaScript 5.
Still, using the constructor pattern and building conventional class hierarchies is mon and helpful, and indeed possible in prototypical languages. Io for example will call an init
method (if existing) everytime you clone
an object, and in above example we could have used one as well which would have made the initialisation of Joe simpler:
person.init = function(f, l) {
this.firstName = f; this.lastName = l; return this;
}
var jo = Object.create(person).init("John", "Doe");
There is definitely no straight line to distinguish between differential and prototypical inheritance.