If I have a function (constructor) with a defined prototype, let's say:
function Student(name) {
this.name = name;
}
Student.prototype.sayHello = function() {
console.log(`Hello, my name is ${this.name}`);
}
And I instantiate and object using that function (from what I know it should set newly created object's property _prototype to the above prototype object in the moment of creation and set constructor property to Student function).
const s1 = new Student('John');
s1.sayHello();
I am sure calling sayHello() would work, however, what if I change the Student's prototype like this?
Student.prototype.sayGoodbye = function() {
console.log(`Goodbye, my name is ${this.name}`);
}
Should s1 be able to call sayGoodbye?
I've read that the _prototype should't change, so in that logic it should throw an error, however, trying it in Chrome's console panel and node.js project it worked and s1 could invoke sayGoodbye. Can someone help me understand this?
Comments
Post a Comment