I have the following piece of code:
function University(name) {
this.name = name
}
University.prototype = {
sayName: function() {
console.log(this.name)
},
toString: function() {
console.log("WUSTL")
}
};
var univ = new University("Washington University");
console.log(univ instanceof University);
console.log(univ.constructor == University); // false
console.log(univ.constructor == Object); // true
can anybody help to explain why the constructor of the instance of 'University' got changed to Object instead of University?
asked Oct 25, 2015 at 2:12
photosynthesis
2,9208 gold badges36 silver badges54 bronze badges
1 Answer 1
Because you overwrote the original prototype which knew the identity of the constructor.
answered Oct 25, 2015 at 2:18
Paul Kienitz
8786 silver badges27 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js
University.prototype.constructor = University;. An useful link about Prototypes.