dopatraman
dopatraman

Reputation: 13908

Prototypal inheritance in javascript, how does it work?

This is my code:

var Quo = function(string) {            //This creates an object with a 'status' property.
    this.status = string;
};
Quo.get_status = function() {
    return this.status;
}
Quo.get_status = function() {
    return this.status;
}

var myQuo = new Quo("confused");        //the `new` statement creates an instance of Quo().

document.write(myQuo.get_status());     //Why doesnt the get_status() method attach to the new instance of Quo?

When I run this code the result is [object Object]. My question what properties of a constructor are inherited by an instance?

Upvotes: 1

Views: 534

Answers (1)

Matt
Matt

Reputation: 44058

My question what properties of a constructor are inherited by an instance?

Anything in Quo.prototype will be available to the instance.

This code should make your example work:

Quo.prototype.get_status = function() {
    return this.status;
};

Quo.prototype.get_status = function() {
    return this.status;
};

Further reading:

Upvotes: 2

Related Questions