I understand what prototype inheritance is, but I should be confused about the implementation. I thought that changing the prototype of the function constructor would affect all instances of this constructor, but it is not. How does JS search for a method from an object to its prototype?
Here is an example
function A(name){
this.name = name;
}
a = new A("brad");
A.prototype = {
talk: function(){
return "hello " + this.name;
}
}
a.talk()
b = new A("john");
b.talk()
I got the impression that I a
would look for a method talk()
in the prototype a
, so any modification of the prototype a
, before or after a
, would be reflected, but it will not. It seems, it is. Can someone explain this to me?
source
share