class Parent{
method(){
console.log("Parent");
}
}
class Child extends Parent{
method(){
console.log("Parent");
}
}
var child = new Child();
console.log(child.method);
console return method in child class which is a expected behaviour.
class Parent{
method = "sss"
}
class Child extends Parent{
method(){
console.log("Child")
}
}
var child = new Child();
console.log(child.method)
why does the console return method variable - "sss" in Parent class ?
From MDN
i.e. it masks the method because it is assigned around the time the constructor runs.
child.methodis a property (with a value that is a string) on the object itself. This masks themethodon the prototype which is a function.The code below demonstrates.
You can see that in example
a(your code) the string value formethodis a property ofchilditself, but you can dig through the prototype chain to get the function value from the class.In example
b(with the public instance field removed), the method exists and can be called, but isn't on thechilditself (because its an instance of the class so it can search the prototype chain automatically because it isn't masked).