我如何拥有同一Javascript模块的多个实例?
假设我有以下模块我如何拥有同一Javascript模块的多个实例?,javascript,Javascript,假设我有以下模块 var TestModule = (function () { var myTestIndex; var module = function(testIndex) { myTestIndex = testIndex; alertMyIndex(); }; module.prototype = { constructor: module, alertMyIndex: function () { alertMyIndex();
var TestModule = (function () {
var myTestIndex;
var module = function(testIndex) {
myTestIndex = testIndex;
alertMyIndex();
};
module.prototype = {
constructor: module,
alertMyIndex: function () {
alertMyIndex();
}
};
function alertMyIndex() {
alert(myTestIndex);
}
return module;
}());
我声明了它的3个实例
var test1 = new TestModule(1);
var test2 = new TestModule(2);
var test3 = new TestModule(3);
我该怎么去
test1.alertMyIndex();
要显示1而不是3?将其指定为此的属性,而不是局部变量
var module = function(testIndex) {
this.myTestIndex = testIndex;
alertMyIndex();
};
然后在
prototype
方法中使用this
引用它。将其指定为this
的属性,而不是局部变量
var module = function(testIndex) {
this.myTestIndex = testIndex;
alertMyIndex();
};
然后在
prototype
方法中使用this
引用它。将其指定为this
的属性,而不是局部变量
var module = function(testIndex) {
this.myTestIndex = testIndex;
alertMyIndex();
};
然后在
prototype
方法中使用this
引用它。将其指定为this
的属性,而不是局部变量
var module = function(testIndex) {
this.myTestIndex = testIndex;
alertMyIndex();
};
然后在prototype
方法中使用this
引用它