Constructor in Javascript

Ohh yes we can define Constructor in Javascript and its very easy.

Constructor is function, which we can defined as follow

function Cat(name){ 
    console.log('this',this);
    this.name=name;
    this.sleep=function(){
        console.log('sleep method has been called');
    },
    this.eat=function(){
        console.log('eat method has been called;');
    }
}

const myCat= new Cat('jeff');

Its Good practise to write first character capital of constructor name as above example I have written Cat.

as I have written

const myCat= new Cat('jeff');

Here, new oprator will create empty object.

Then it will set all properties to this instance.

Finally it will return Cat object from this function, that's why we don't need to write return statement here.