我想达到的一个构造函数扩展方法,并用它来初始化实例,如:混淆有关原型继承在JavaScript
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
是对的。
那么,我的代码有什么问题?为什么原型消失?我该怎么做对不对?
呦使用[的Object.create您自担风险使用它](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create)now – aaronman
@aaronman:我试着自己做一个 – hh54188