1

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?

1 Answer 1

2

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:

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

7 Comments

@Matt--what properties are contained in the prototype object by default?
@codeninja - it's a chain. By default you have everything from Object.prototype, but it is possible to "extend" from others. Take a peek at the MDC articles I linked
@Matt--so when Quo is created, Quo.prototype is empty? Or does it contain the properties of Quo? note: i havent looked at the MDN articles yet.
@codeninja - when you create Quo itself (NOT the instances), the Quo.prototype is technically empty. Prototypes are chains, so when a quo instance doesn't find what it wants, it looks in the next prototype in the chain (in this case, it looks in Object.prototype).
@codeninja - np. on your way out, just to give you an idea of the flexibility of prototypal inheritance .. prototypes are checked dynamically at property-lookup-time. So you can modify Quo.prototype at any time and all instances (future and existing) will be affected.
|

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.