お気づきのように、JS での継承は注意が必要です。おそらく、私よりも賢い誰かが技術的な詳細について教えてくれますが、考えられる解決策は、 Dead Edwardsの厚意により、非常に小さな Base.jsフレームワークを使用することです。
編集: Dean Edward のフレームワークに合うように元のコードを再構築しました。
構文をマスターすると、継承は適切に機能します。コードに基づく可能な解決策は次のとおりです。
var View = Base.extend({
constructor: function(location) {
if (location) {
this.location = location;
}
},
location: "UK",
getLocation: function() {
return this.location;
}
});
そしてそれを拡張します:
var Paper = View.extend({
location: "US"
});
そしてそれをテストします:
var view = new View();
alert("The current location of the view is: " + view.getLocation());
var paper = new Paper();
alert("The location of the paper is: " + paper.getLocation());
alert("The current location of the view is: " + view.getLocation());
または、次の方法でも同じ結果が得られます。
var Paper = View.extend();
そしてテスト:
var view = new View();
alert("The current location of the view is: " + view.getLocation());
var paper = new Paper("US");
alert("The location of the paper is: " + paper.getLocation());
alert("The current location of the view is: " + view.getLocation());
どちらも次の 3 つのアラートを生成します。
The current location of the view is: UK
The location of the paper is: US
The current location of the view is: UK
これが役立つことを願っています!