I notice in MDN they said: "redefining the prototype is not recommended" and they gave an example of redefining the prototype and an example of how to better do it by appending to the existing prototype instead of redefining the prototype.
I especially wondered about this because of other cases like this where we need to redefine the prototype:
Cube.prototype = Object.create(....)
Manager.prototype = new Employee;
Is it that MDN is saying that you should not redefine the prototype for the situations like the example they gave (shown below) but it is in fact OK to redefine it for situations like I just mentioned above?
Here (paraphrased) is what they said on the MDN page:
//MDN says this is not the best way since it redefines the prototype
function MyObject(name, message) {
this.name = name.toString();
this.message = message.toString();
}
MyObject.prototype = {
getName: function() {
return this.name;
},
getMessage: function() {
return this.message;
}
};
//MDN says this is better to do it this way since this does not
//redefine the prototype:
function MyObject(name, message) {
this.name = name.toString();
this.message = message.toString();
}
MyObject.prototype.getName = function() {
return this.name;
};
MyObject.prototype.getMessage = function() {
return this.message;
};
My questions: is this correct and if so can you shed some under-the-hood details to answer why. Especially looking at the MDN example -- why is one way actually better than the other way?
One quick note: I love MDN and appreciate all the work on that site! My comments above are just a question so I can understand the rules of redefining the prototype and not in any way a criticism to what they said.