var Foo = (function () {
var cls = function () {
this.prototype = {
sayhi: function () {
alert('hi');
}
};
};
cls.staticMethod = function () {};
return cls;
})();
var f = new Foo();
为什么我无法访问我的sayhi
方法? this
不引用cls
变量吗?
答案 0 :(得分:1)
您正尝试在prototype
的每个实例上设置cls
属性。您真正想要做的是设置prototype
本身的cls
属性:
var Foo = (function () {
var cls = function () {}; // Constructor function
cls.prototype = { // Prototype of constructor is inherited by instances
sayhi: function () {
alert('hi');
}
};
return cls;
})();