Prototypal inheritance in javascript, how does it work?
This is my code:
var Quo = function(string) { //T开发者_Python百科his 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?
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:
- https://developer.mozilla.org/en/JavaScript/Guide/Inheritance_constructor_prototype
- https://developer.mozilla.org/en/JavaScript/Guide/Inheritance_Revisited
精彩评论