JavaScript Prototype Part 2 : Inheritance (Classical Approach)<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

JavaScript Prototype Part 2 : Inheritance (Classical Approach)

Posted on: May 15, 2017

In this article, we will see how to create inheritance with JavaScript's prototype in a classical approach.

We will create a base class and a child class. The base class will have 3 functions on its prototype. We will override one of these functions inside the child class in two different ways. The first one will override by a function attached to the instance, the second one will override the function attached to the prototype.

To have inheritance with JavaScript there is two steps once the class are created.

The first step is to call the base class constructor with the the this parameter. This is done by using the name of the function (the class) followed by "call" with this as parameter.

The second step is to set the prototype of the child class to an instance of a new base object. This is done my calling assigning the prototype to Object.Create(.prototype). An important detail is that this must be set before you associate any new prototype to the object, otherwise, every prototype defined will be overridden.

The third step is to set the prototype of the child class to be setup to the childclass. That is that the Object.create set the prototype to the base class which need to set back to the child. We can see that without setting this constructor that the value of console.log(ChildClass1.prototype.constructor) is the base class instead of the child class.

1var BaseClass1 = function () {
2 this.memberbase = "base";
3 this.function4 = function () {
4 return "return function4 from base instance";
5 };
6};
7
8BaseClass1.prototype.function1 = function () {
9 return "return function1 from base prototype";
10};
11
12BaseClass1.prototype.function2 = function () {
13 return "return function2 from base prototype";
14};
15
16BaseClass1.prototype.function3 = function () {
17 return "return function3 from base prototype";
18};
19
20var ChildClass1 = function () {
21 BaseClass1.call(this); // Call the constructor of the base class [Step1]
22 this.memberclass = "Child";
23 this.function2 = function () {
24 return "return function2 from child";
25 };
26};
27
28ChildClass1.prototype = Object.create(BaseClass1.prototype);
29
30// [Step2]
31ChildClass1.prototype.constructor = ChildClass1;
32
33// [Step3]
34
35ChildClass1.prototype.function1 = function () {
36 // Must be after previous line return "return from child";
37};
38
39var instance1 = new ChildClass1();
40
41console.log("instance1.function1: " + instance1.function1()); // override with prototype of child
42console.log("instance1.function2: " + instance1.function2()); // override with instance function
43console.log("instance1.function3: " + instance1.function3()); // Use base class function
44console.log("instance1.function4: " + instance1.function4()); // Use base class function
45//console.log(ChildClass1.prototype.constructor) // uncomment to see what step 3 change.

The output is :

1"instance1.function1: return from child"
2"instance1.function2: return function2 from child"
3"instance1.function3: return function3 from base prototype"
4"instance1.function4: return function4 from base instance"

What we can see is that the chain of priority when calling a function start with the child instance function, move up to the child prototype function, then to the base prototype function. So, it's possible to redefine a base member in 3 different levels.

The priority will always be : Child instance, Base instance, Child prototype, Base prototype.

We can prove it with this example:

1var BaseClass1 = function () {
2 this.function1 = function () {
3 return "Base Instance";
4 };
5};
6
7BaseClass1.prototype.function1 = function () {
8 return "Base Prototype";
9};
10
11var ChildClass1 = function () {
12 BaseClass1.call(this); // Call the constructor of the base class
13 this.function1 = function () {
14 return "Child Instance";
15 };
16};
17
18ChildClass1.prototype = Object.create(BaseClass1.prototype);
19ChildClass1.prototype.function1 = function () {
20 // Must be after previous line return "Child Prototype";
21};
22
23ChildClass1.prototype.constructor = BaseClass1;
24var instance1 = new ChildClass1();
25console.log("instance1.function1: " + instance1.function1());

This will print : "instance1.function1: Child Instance".

If we remove this instance function, this will print : "instance1.function1: Base Instance"

If we remove the base instance function, this will print: "instance1.function1: Child Prototype"

If we remove the base prototype function, this will print: "instance1.function1: Base Prototype"

In this last example, we were using Object.create, but also new to create the instance that we manipulate the data. If we want to avoid completely the "new", we need to create the child class with Object.Create. This is possible, but will require to change the child class. For example, we won't specify function 1 inside ChildClass1 function, neither call BaseClass with call. The code would look like:

1var BaseClass1 = function () {
2 this.function1 = function () {
3 return "Base Instance";
4 };
5};
6
7BaseClass1.prototype.function1 = function () {
8 return "Base Prototype";
9};
10
11var ChildClass1 = Object.create(BaseClass1.prototype, {
12 name3: { value: "MyName3" },
13 function1: {
14 value: function () {
15 return "Child Instance";
16 },
17 },
18});
19
20ChildClass1.function1 = function () {
21 // Must be after previous line return "Child Prototype";
22};
23
24var instance1 = Object.create(ChildClass1, {
25 memberclass: { value: "Child" },
26});
27
28console.log("instance1.function1: " + instance1.function1());