Advanced JavaScript Concepts:

javascript dev.to

JavaScript is a scripting language and advanced JavaScript is magical. Unlike, other programming languages, concepts of Inheritance, Constructors and Prototype are not the same. Let’s have a brief understanding of these concepts in detail:

1.Inheritance

In JavaScript, concepts of Inheritance are implemented by objects where objects are interlinked with each other.
obj 1 -> interlinked -> obj 2
Here, the object’s properties are stored inside the prototype and once, the prototype is null, the interlinking is stopped. This interlinking process is known as PROTOTYPE CHAIN. Once, the value of the prototype of an object is null, the link is terminated.
obj 1 (prototype 1) -> obj 2 (prototype 2) ->
obj 3 (prototype 3) -> obj 4(null)

But what if we need or check the prototype of the object, how we can check. There is one method Object.getPrototypeOf() of functionName.prototype. There is no class based inheritance and here, functions are added as property inside the object.

Longer inheritance chains are more error prone.

2. Constructors

Constructors are used to re-use the properties present in each instance which is also, beneficial as it lowers the memory usage. The construction function is called using a new keyword.

The constructor function creates the instance by using the constructor’s prototype.
Object.getPrototypeOf(Constructor) === Function.prototype

And, Constructor.prototype is used for constructing instances.

Though, while creating objects two processes can be used,
Object.create and using proto keyword.

As per the understanding of code, Object.create is less performant than proto.

Object.create() sets the prototype of the object at the creation time only which helps in optimization of the object.
Object.create() has non-enumerable properties.
Object.create(), object creation is less error prone.
Object.create() is slower than object literals.

To check whether the object has its own property or inherited from another we can have two methods for it.

Object.hasOwn() used to check whether the object has its own property.
Object.hasOwnProperty() used to check the object has inherited it from others.

This is some understanding of advanced topics of JavaScript and will be helpful for programming using JavaScript.

Source: dev.to

arrow_back Back to Tutorials