Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is differential inheritance in JavaScript?

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 comment on that answer some time back but I didn't receive any replies. So wanted to check with the larger and awesome community of SO JavaScript users.

like image 651
Geek Avatar asked Jul 21 '13 11:07

Geek


People also ask

What are the types of inheritance in JavaScript?

Mainly there are three types of inheritance in JavaScript. They are, prototypal, pseudo classical, and functional.

What is inheritance in JavaScript?

Inheritance enables you to define a class that takes all the functionality from a parent class and allows you to add more. Using class inheritance, a class can inherit all the methods and properties of another class. Inheritance is a useful feature that allows code reusability.

What is prototypal inheritance in JavaScript example?

The Prototypal Inheritance is a feature in javascript used to add methods and properties in objects. It is a method by which an object can inherit the properties and methods of another object. Traditionally, in order to get and set the [[Prototype]] of an object, we use Object. getPrototypeOf and Object.

What type of inheritance does JavaScript natively support?

Inheritance is an important concept in object oriented programming. In the classical inheritance, methods from base class get copied into derived class. In JavaScript, inheritance is supported by using prototype object.


1 Answers

As the other commenters 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 common 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 common 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.

like image 88
Bergi Avatar answered Sep 19 '22 14:09

Bergi