1

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?

1
  • You need to reset the prototype whenever you overwrite the it: University.prototype.constructor = University;. An useful link about Prototypes. Commented Oct 25, 2015 at 2:22

1 Answer 1

3

Because you overwrote the original prototype which knew the identity of the constructor.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.