📄 Table of Contents

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬✦✦✦ ▬▬▬▬▬▬▬▬▬▬▬▬▬▬

◉ “4” Pillars of Inheritance

◉ Abstraction

In jQuery, we will use $(“#elem”) to select an element with id `elem` on a web page. Actually this code calls native JS code.

document.getElementById("elem")

but we don’t need to know & can happily use the $(“#elem”) without knowing inner details of the implementation.

◉ Encapsulation

Encapsulation can be implemented using by access modifier i.e. private, protected and public.

function Person() { // private variables
var dob = "21-09-1988";
//public properties and methods return{
age: "30",
name: "ak",
getDOB: function () {
return dob;
}
}
}
var foo = new Person();
console.log(foo.dob); # undefined
console.log(foo.getDOB()); # 21-09-1988

◉ Polymorphism

In JS, an object, property, method can have more than one form. Polymorphism is a very cool feature for dynamic binding and late binding.

◉ Inheritance

Let me put light on thought:

Classes are just syntactic sugar, JavaScript still remains prototype based. They are not a new object-oriented inheritance model.

Javascript follows the prototype mechanism to inherit properties and methods.

When we call the property or method of an object, the browser checks to see if the actual object has the method on it, if NOT it checks if the method is available to it through its prototype object.

We create sportsCar and provide properties of brandName, topSpeed, and color. Then we added a property called gearType to the Car prototype object and assigned the value ‘automatic’ to it.

When we call any defined object property, it will give you the value because it is available in the object itself, and when we call gearType it will also give you the value but, the only difference is it is available in the objects constructor prototype.

But if we check if either sportsCar.hasOwnProperty(‘gearType’) and Car.hasOwnProperty(‘gearType’) of ‘gearType’ and both return false, but Car.prototype.hasOwnProperty(‘gearType’) return true.

That is a Javascript prototype-based inheritance.

[1]. Prototypal inheritance using Object.create() & call()

source: Source: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Inheritance

OR

Father.call(this);

Note that we’ve only specified “this” inside call() — no other parameters are required as we are not inheriting any properties from the parent that are set via parameters.

◉ JavaScript Inheritance with “Object.create”

Let’s see this method in Details.

In the above code, we are using “Object.create”, to create a new Object. Since we haven’t adding any property or Method to the new Object therefore the Object “newObject” is still empty having no method or properties.

See the image below.

The output above shows that none of the property is available to the newly created object. It still do not contain any method or properties.

But, we are passing “userInfo” object to “Object.create” function as a parameter. “userInfo” Object contains “name”, “age”, “designation” and “salary” property along with their values. Let’s look into the impact of passing “userInfo” as a parameter to the function.

Can we access those property using new Object ?

When we create Object using “Object.create”, the Object passed as parameter is added to the “prototype” chain of the newly created Object. When user try to access the property from the new Object created, it looks for the prototype chain available to the new Object. The JavaScript Object can traverse “prototype” chain using “__proto__” property.

The “userInfo” object passed to Object.create function was added to “__proto__” property of the new Object.

Let’s see how can we access this prototypal chain of the Object.

When we try to access the property “name” using “newObject”, it first looks for the properties that are available to this Object. Since we do not have any property added to this Object, it traverses to the “prototype” chain using the Object reference “__proto__”. The “__proto__” property points to the reference of “userInfo” object.

Since the properties “name” is available in the Object which is pointed by “__proto__” property, we can access the property “name” using the newly created Object. The properties “name”, “age”, “designation” and “salary” is available to the new Object using the prototypal chain.

In short, it represents the Parent Child relationship, if the property in not found in an Object being referred, it traverses its prototype chain to find the property/method in the Parent Object. If the property/method is available in the Prototype Chain, we can access those property and methods.

The above lines of code, we are using “===”, it state that “userInfo.name” and “newObject.__proto__name” point to the same memory location.

Accessing Method of the Prototypal Chain

Overriding the values of Prototype Chain

In the above code, we can see that, we are adding the property to the newly created Object. In this case the Object will be having a new property available to itself and it will access/modify its own property, rather than referring to the parent property.

In the image above, we can see that the “name” property is added to the newly created Object “newObject”.

Lets look into Advantages and Disadvantages of this Method:

Advantages of Object.create

The Property is made available through the prototypal chain, so the object need not to consume more memory space to save the existing Functions and Properties. No separate memory space is consumed. So the methods and the properties can be reused.

This method provides function and property re-usability in cases when we have some hierarchical data representation / or if we need to show some inheritance traits from Source Object to Destination Object

Disadvantages of “Object.create”

The developer may assume that the property belongs to the new Object created. Whereas, modifying the property or Function in Source Object are reflected in Destination/new Object Created.

If you want to have dedicated ”name” property to each Object created using “Object.create”, we need to add property explicitly to newly created Object. In the below code, we have added separate property to the new Object Created, So in this case it will not access the “name” data from Prototype Chain.

2. Do not create the Deep Copy for the Object

It do not create the Deep copy, the Object follow the Concept of Inheritance. So the new Object do not contains separate properties and methods. The properties and methods are accessed from the parent in the Inheritance Chain.

Object.create() Exercises

Object.create() Exercise
Object.create() Exercise

[2]. Classical inheritance using Extends() & super()

Let’s understand how this works, internally extends keyword works using the prototype mechanism.

class Son extends Father 
// this will get converted into the prototype object i.e.
Son.prototype.__proto__ will be Father.prototype

In our case: Son.prototype.__proto__ = Father.prototype

So if the method is not found in Son class, Javascript takes it from the Father class.

Javascript is prototype-based only, in class inheritance also, it works on top of prototypal inheritance (mechanism).

Overriding constructor

Let’s say Son (derived class) doesNot have any constructor rather inheriting from Father (parent class).

According to the specification, if a class extends another class and has no constructor, then the following “empty” constructor is generated:

Derived Class with no constructor of its own

Prototypal inheritance may take some time getting used to, but it’s an important concept to grasp in order to master OOP in vanilla JavaScript. While it is often considered to be one of JavaScript’s weaknesses, the prototypal inheritance model is in fact more powerful than the classic model. It is, for example, fairly trivial to build a classic inheritance model on top of a prototypal model.

ECMAScript 6 introduced a new set of keywords implementing classes. Although these constructs looks like class-based languages, they are not the same. JavaScript remains prototype-based.

◉ is constructor mandatory in a class?

if you do NOT specify a constructor method a default constructor is used. For base classes the default constructor is:

constructor() {}

For derived classes, the default constructor is:

constructor(...args) {
super(...args);
}

◉ Composition over Inheritance (recommended in React)

Composition is recommended over Inheritance in React

◉ Private Members in JavaScript

Objects

If a value is a function, we can consider it a method. When a method of an object is invoked, the this variable is set to the object. The method can then access the instance variables through the this variable.

Objects can be produced by constructors, which are functions which initialize objects. Constructors provide the features that classes provide in other languages, including static variables and methods.

➪ Public

🔹In the constructor

function Container(param) {
this.member = param;
}

So, if we construct a new object

var myContainer = new Container('abc');

then myContainer.member contains ‘abc’.

🔹 In the prototype

Container.prototype.stamp = function (string) {
return this.member + string;
}

So, we can invoke the method

myContainer.stamp('def')

which produces ‘abcdef’.

Private and privileged members can only be made when an object is constructed.

Public members can be added at any time.

➪ Private

function Container(param) {
this.member = param;
var secret = 3;
var that = this;
}

This constructor makes three private instance variables: param, secret, and that. They are attached to the object, but they are not accessible to the outside, nor are they accessible to the object’s own public methods. They are accessible to private methods.

Private methods are inner functions of the constructor.

function Container(param) {

function dec() {
if (secret > 0) {
secret -= 1;
return true;
} else {
return false;
}
}

this.member = param;
var secret = 3;
var that = this;
}

The private method dec examines the secret instance variable. If it is greater than zero, it decrements secret and returns true. Otherwise it returns false. It can be used to make this object limited to three uses.

By convention, we make a private that variable. This is used to make the object available to the private methods. This is a workaround for an error in the ECMAScript Language Specification which causes this to be set incorrectly for inner functions.

Private methods cannot be called by public methods.

To make private methods useful, we need to introduce a privileged method.

➪ Privileged

Privileged methods are assigned with this within the constructor.

function Container(param) {

function dec() {
if (secret > 0) {
secret -= 1;
return true;
} else {
return false;
}
}

this.member = param;
var secret = 3;
var that = this;

this.service = function () {
return dec() ? that.member : null;
};
}

service is a privileged method. Calling myContainer.service() will return ‘abc’ the first three times it is called. After that, it will return null. service calls the private dec method which accesses the private secret variable. service is available to other objects and methods, but it does not allow direct access to the private members.

Closures

Patterns

🔹Public

function Constructor(...) {
this.membername = value;
}
Constructor.prototype.membername = value;

🔹 Private

function Constructor(…) {
var that = this;
var membername = value;
function membername(…) {…}
}

Note: The function statement

function membername(...) {...}

is shorthand for

var membername = function membername(...) {...};

🔹Privileged

function Constructor(...) {
this.membername = function (...) {...};
}

References:

Experience with Front-end Technologies and MERN / MEAN Stack. Working on all Major UI Frameworks like React, Angular.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store