class syntactic sugar
class itself is a function
class MathHandle(){
builder (x, y) {
this.x = x
this.y = y
}
add(){
return this.x+this.y
}
}
const m = new MathHandle(1,2)
console.log(m.add(1,2))
js constructor usage:
function MathHandle(x,y){
this.x = x;
this.y = y;
}
MathHandle.prototype.add = function(){
return this.x+this.y
}
const m = new MathHandle(1,2)
console.log(m.add(1,2))
Constructors MathHandle
MathHandle.prototype.constructor === MathHandle
m.__proto__ === MathHandle.prototype
class inheritance
class Animal{
constructor(name){
this.name = name
}
eat(){
console.log(`${this.name}eat`)
}
}
class Dog extends Animal{
constructor(name){
super(name)
this.name = name
}
say(){
console.log(`${this.name}say`)
}
}
const dog = new Dog ( 'Husky')
dog.say()
dog.eat()
Note: Be sure to follow the wording extends it super write on the back of the class in the constructor