次のように、コンストラクター関数でextendメソッドを実現し、それを使用してインスタンスを初期化したいと考えています。
var view_instance = View.extend()
だから私はこれを試しました:
define(function (require, exports, module) {
function View(size, color) {
this.size = size;
this.color = color;
};
View.prototype = {
show: function () {
console.log("This is view show");
}
}
View.extend = function (props) {
var tmp = new this("12", "red");
console.log(this);
console.log(tmp instanceof this) //true
console.log(tmp.prototype); //undefined!
return tmp
}
return View;
})
上記のextend
方法では、 を使用してインスタンスを初期化しましたnew this()
が、そのプロトタイプをログに記録できず、this
が正しいことを確認しました。
では、私のコードの何が問題なのですか? なぜプロトタイプは消えたのですか?どうすれば正しくできますか?