I see this code below.
I expect the monkey instance should use the child sayName method, but actually, not like the normal, this instance uses the parent Animal.
Can some one explain why?
class Animal {
sayName = () => {
console.log("parent animal")
}
}
class Monkey extends Animal {
sayName() {
console.log("monkey, child, extend parent")
}
}
const monkey = new Monkey();
monkey.sayName()
This is not a wrong code! The first one is arrow function and second one is not one arrow function.
I only want to know why in this way, the instance uses the parent method rather than the normal the extends one.