2011-08-04 6 views
1

これは私のコードです:私はこのコードを実行するとjavascriptのプロトタイプ継承はどのように機能しますか?

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? 

結果は[object Object]です。私の質問は、コンストラクタのプロパティは、インスタンスによって継承されていますか?

答えて

2

私の質問コンストラクタのどのプロパティがインスタンスによって継承されていますか?

Quo.prototypeのすべてのものがインスタンスで使用可能になります。

このコードは、あなたの例の作業を行う必要があります

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

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

さらに読書:

+0

- どのようなプロパティがプロトタイプオブジェクトに含まれていますデフォルトでは? – dopatraman

+0

@codeninja - チェーンです。デフォルトでは、 'Object.prototype'の全てがありますが、他のものから「拡張」することも可能です。私が – Matt

+0

@ MattとリンクしたMDCの記事を見てください。そうすれば、Quoが作成されたとき、Quo.prototypeは空ですか?それともQuoのプロパティを含んでいますか? 注:私はまだMDNの記事を見ていません。 – dopatraman

関連する問題