Understanding constructor, super, __proto__ and prototypes

PHOTO EMBED

Sat Jun 18 2022 11:23:42 GMT+0000 (Coordinated Universal Time)

Saved by @mehran1801 #react.js

/* 
1. A constructor is a function that creates an instance of a class 
 which is typically called an “object”.

 2. In JavaScript, a constructor gets
 called when you declare an object using the new keyword.

 3. The purpose of a constructor is to create an object and 
 set values if there are any object properties present.
*/
function Person(first, last, age, eye) {
  this.firstName = first;
  this.lastName = last;
  this.age = age;
}

// Create a Person object
const myFather = new Person("John", "Doe", 50);
console.log(myFather)
/*
Inheritance is when we create a parent class with properties and methods
that we can extend to child classes.
We use the extends keyword to create a subclass.
The super keyword calls the constructor() of a parent class.
*/

/*
Remember Functions are function-object combo, In the object bit of 
function we put all the methods in a default property(which every
object has) called prototype and it is itself an object. 

In ES6 class keyword puts them in prototype object for us automatically
and the function bit of function-object combo, we rename it 
"constructor".
Every object has hidden property called __proto__ which makes link to 
prototype in the objects chain. That's how we access shared functions.

Methods should never be comma-separated, if inside of a class body. This is to emphasize the fact that classes and object literals are different.
*/



content_copyCOPY