为什么我的 JavaScript 实例返回相同的内容
Why does my JavaScript instances return the same?
我正在尝试创建一个名为myObjectConstructor
的函数变量的两个实例(constructorOne
和constructorTwo
(。出于某种原因,当我调用getProperty
时,我为两个实例返回相同的结果。
//this is one other way of creating a Constructor function
var myObjectConstructor = function(){
this.myProperty = '';
init = function(str) {
this.myProperty = str;
},
getProperty = function() {
return this.myProperty;
}
return {
init: function () {
return init.apply(self, arguments);
},
getProperty: function () {
return getProperty.apply(self, arguments);
}
}
}
//instantiate our Constructor
var constructorOne = new myObjectConstructor();
//change myProperty of the first instance
constructorOne.init('this is property one');
//instantiate a second instance of our Constructor
var constructorTwo = new myObjectConstructor();
constructorTwo.init('this is property two');
constructorOne.getProperty()
和constructorTwo.getProperty()
都会提醒">这是属性二":
alert(constructorOne.getProperty()); //this will alert 'this is property two'
alert(constructorTwo.getProperty()); //this will still alert 'this is property two'
这是一个演示。
问题是,为什么constructorOne.getProperty()
不返回"这是财产一"?
self
undefined
,所以在init函数中,this
是window
的,所以你总是在修改同一对象的属性。
您似乎正在做一些模块模式和标准JavaScript构造函数的奇怪混合。我强烈建议选择两种方法中的一种并坚持下去。
function MyObjectConstructor(){
this.myProperty = '';
}
MyObjectConstructor.prototype.init = function(str) {
this.myProperty = str;
};
MyObjectConstructor.prototype.getProperty = function() {
return this.myProperty;
};
//instantiate our Constructor
var constructorOne = new MyObjectConstructor();
//change myProperty of the first instance
constructorOne.init('this is property one');
//instantiate a second instance of our Constructor
var constructorTwo = new MyObjectConstructor();
constructorTwo.init('this is property two');
//alert current myProperty of constructorOne instance
alert(constructorOne.getProperty());
//alert current myProperty of constructorTwo instance
alert(constructorTwo.getProperty());
或
function myModule (){
var myProperty = '';
return {
init: function (str) {
myProperty = str;
},
getProperty: function () {
return myProperty;
}
};
}
//instantiate our Constructor
var constructorOne = myModule();
//change myProperty of the first instance
constructorOne.init('this is property one');
//instantiate a second instance of our Constructor
var constructorTwo = myModule();
constructorTwo.init('this is property two');
//alert current myProperty of constructorOne instance
alert(constructorOne.getProperty());
//alert current myProperty of constructorTwo instance
alert(constructorTwo.getProperty());
更好的方法是使用适当的原型:
function MyObjectConstructor() {
this.property = '';
}
MyObjectConstructor.prototype.init = function(newProperty) {
this.property = newProperty;
};
MyObjectConstructor.prototype.getProperty = function() {
return this.property;
};
相关文章:
- 从我的控制器返回一个不同于200的代码以触发ajax错误,这被认为是一种好的做法吗
- 我希望只有在我滚动页面后才能显示我的返回页首图标
- 我可以't返回我的json对象
- Angular 不返回我的自定义 JSON 文件
- 主干返回我的 JSON,但没有获取值
- AJAX:响应文本在本地返回我的整个PHP代码
- Backbone fetch返回我的模型的所有实例
- 如何返回我的表单错误
- responseText正在返回我的php文件的顶部
- 我怎么能等待谷歌远程呼叫返回我的结果之前继续
- 在.remove()和.append()之后返回我的事件按钮
- 上传onUploadSuccess没有返回我的数据
- 为什么我的jQuery不返回我的方程的值?
- 我如何返回我的项目在javascript和ASP.NET的当前域
- 如何正确地返回我的数据从AJAX调用(AngularJS)
- 为什么jquery没有返回我的值?
- Jquery点击函数,使用这个但返回我的窗口对象
- JSON 返回我的标头数据
- Instagram api没有返回我的以下列表
- Js: GetElementByID()不返回我的元素