javaScript's - inheritance, super keyword, new keyword
JavaScript’s (OOJS)
Today’s topic - Inheritance, Super keyword, New keyword.
Inheritance -
Inheritance basically means to create a child class which has access to a parent class or base class. The child class inherits all the methods from its parent class.
To create a class inheritance we use the Extends keyword.
Inheritance is basically great for code reusability.
WHY?
Because you can use the properties and methods of the parent class in the child class.
Super keyword -
So basically the super() keyword is only available to classes. The super() always refers to the parent class constructor.
By calling the super() method in the constructor of the child class you call the parent class constructor and get access to parent properties and methods.
If there is a constructor in the child class the super() method should be used before this() keyword otherwise it will cause a reference error.
New keyword -
The new keyword is basically used to create an instance of an object through a constructor method.
The new keyword does the following things behind the scenes.
Creates a new object.
Sets the prototype (__proto__) of this newly created object as same as the constructor function’s prototype.
Binds this keyword with the newly created object and executes the constructor function.
Returns a newly created object.
Eg - Inheritance, super, new.
Hoisting -
Hoisting is basically that the javaScipt goes through code code before executing it and stores all the function declarations in the memory. So it allows you to use the function before their declaration in your code.
But it is not the same with classes. Hoisting is not available for classes you cannot use a class before it’s declaration. And if you do you know you’ll get your favorite thing an “ERROR” yup!
Summary
Inheritance means using functionality of the base class in the derived class by using the extend keyword.
super() keyword is used to call the parent constructor method and get access to its properties and method in derived class.
New keyword is used to create an object from the constructor function.
Hoisting means that the functions can be used before their declaration but it’s not the same with the classes.
set yourself free from" ;" by using python ✌
ReplyDelete