how to add methods to constructor in javascript code example
Example 1: javascript constructor function
function ClassMates(name,age){
this.name=name;
this.age=age;
this.displayInfo=function(){
return this.name + "is " + this.age + "year's old!";
}
}
let classmate1 = new ClassMates("Mike Will", 15);
classmate.displayInfo(); // "Mike Will is 15 year's old!"
Example 2: why do we use Object Constructors
function Tree(name) {
this.name = name
}
let theTree = new Tree('Redwood')
console.log('theTree.constructor is ' + theTree.constructor)