[英]variable in my javascript class is undefined when using an instance of that class
I have declared this javascript class: 我已经声明了这个javascript类:
var TablixColumns = function(){
this.g = 'wtf';
this.tablixColumns = []; //[];
return {
addTablixColumn: function(tablixColumn){
alert(this.g);
//this.tablixColumns.push(tablixColumn.getTablixColumn());
}
}
};
my problem is that when I try this: alert(this.g)
the alert comes out undefined of course my initial function definition read: this.tablixColumns.push(tablixColumn.getTablixColumn());
我的问题是,当我尝试这样做时:
alert(this.g)
警报当然是不确定的,我的初始函数定义为: this.tablixColumns.push(tablixColumn.getTablixColumn());
but then I get the error that reads something like "No Method push
of undefined" 但是然后我得到的错误显示为“未定义的无方法
push
”
what's weird is I have this class declaration: 奇怪的是我有这个类声明:
var TablixColumn = function(){
columnWidth = '<Width>3.135cm</Width>'; //default width value
return{
setColumnWidth: function(width){
this.columnWidth = '<Width>' + width + 'cm</Width>';
},
getTablixColumn: function(){
return '<TablixColumn>' + this.columnWidth + '</TablixColumn>';
}
}
};
and the TablixColumn class works fine, and yes, I have declared this.g and this.tablixColumns without the 'this.' 并且TablixColumn类可以正常工作,是的,我已经声明了this.g和this.tablixColumns,而没有使用“ this”。 part, but it's just refusing to work!
部分,但这只是拒绝工作! I'm going to kill someone if this doesn't work tonight can someone help me please?
如果今晚行不通,我要杀人,有人可以帮我吗?
You need to set a reference to the current object ( this
) outside the nested function expression. 您需要在嵌套函数表达式之外设置对当前对象(
this
)的引用。 Here's how your code should look: 这是代码的外观:
var TablixColumns = function() {
...
var self = this;
return {
addTablixColumn: function(tablixColumn) {
alert(self.g);
}
};
};
You can even set a property to the returned object literal if you want: 您甚至可以根据需要为返回的对象文字设置属性:
// ...
return {
g: 'wtf',
addTablixColumn: function(tablixColumn) {
alert(this.g); // 'wtf'
}
};
// ...
Note that you shouldn't use TablixColumns
as a constructor if you're returning from it like this. 请注意,如果您要从
TablixColumns
这样返回,则不应将其用作构造函数。 You're using two idioms here; 您在这里使用了两个习语; prototypal inheritance and the module pattern.
原型继承和模块模式。 Are you going to instantiate the constructor with
new
? 您要使用
new
实例化构造函数吗? If so, then don't return the object literal. 如果是这样,则不要返回对象文字。 Rather, set the methods on the prototype of the function:
而是在函数的原型上设置方法:
var TablixColumns = function() {
this.g = 'wtf';
this.tablixColumns = [];
};
TablixColumns.prototype.addTablixColumn = function addTablixColumn() { ... };
TablixColumns.prototype./* ... */
...
Otherwise, don't use this
inside the constructor. 否则,请勿在构造函数中使用
this
函数。 Simply make the properties normal variables. 只需将属性设置为普通变量即可。
Okay guys so I figured out my problem: 好的,所以我想出了我的问题:
all the references to variables of the current instance should not have been preceded by the this.
当前实例的所有对变量的引用都不应在
this.
之前this.
keyword so this is how my declaration looks now: 关键字,因此这是我的声明现在的样子:
var TablixColumns = function(){
g = 'wtf';
tablixColumns = []; //[];
return {
addTablixColumn: function(tablixColumn){
alert(g);
tablixColumns.push(tablixColumn.getTablixColumn());
}
}
};
Thanks @Bergi for pointing this out 感谢@Bergi指出这一点
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.