declaring methods in constructor funcitons code example
Example 1: javascript class constructor
class Cat {
constructor(name, age) {
this.name = name;
this.age = age;
}
get fullname() {
return this.getFullName()
}
getFullName() {
return this.name + ' ' + this.age
}
}
const run = document.getElementById("run");
run.addEventListener("click", function () {
let Skitty = new Cat('Skitty', 9);
let Pixel = new Cat('Pixel', 6);
console.log(Skitty.getFullName());
console.log(Skitty.fullname);
console.log(Skitty, Pixel);
})
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)