javascript: 变量未定义
javascript: variable undefined
当我在 javascript 中使用变量时,出现错误
ReferenceError: getalletje is not defined
,取决于我在哪里声明它。
这是我声明变量的地方:
get_name: function(){
var getalletje = 5;
return getalletje;
},
我尝试使用变量的地方:
self.$('.geldinuit-button').click(function(){
self.screen_selector.show_popup('geldinuit',{
message: _t('Popup titel'),
comment: _t('getalletje'),
confirm: function(){
window.alert(getalletje);
},
});
});
它给出了这样的错误。
但是:如果我将 var getalletje = 5;
放在 self.$('.geldinuit-button').click(function(){
的正上方,它会起作用。
我还需要做些什么吗?
为 Shomz 编辑:这是完整代码:
.............
self.set_smart_status(status.newValue);
});
this.$el.click(function(){
self.pos.connect_to_proxy();
});
},
});
module.PosWidget = module.PosBaseWidget.extend({
template: 'PosWidget',
init: function() {
this._super(arguments[0],{});
this.pos = new module.PosModel(this.session,{pos_widget:this});
this.pos_widget = this; //So that pos_widget's childs have pos_widget set automatically
.............................
............................
get_name: function(){
var getalletje = 5;
return getalletje;
},
...........................
...........................
self.$('.geldinuit-button').click(function(){
self.screen_selector.show_popup('geldinuit',{
message: _t('Popup titel'),
comment: _t('getalletje'),
confirm: function(){
window.alert( this.get_name() );
},
});
});
..........................
你不能在声明它的函数之外访问变量。如果你想以全局方式访问变量,你应该在 get_name 之外声明它。
在你的情况下,我不明白这一点,但以下应该有效:
var getalletje = 0;
get_name: function(){
getalletje = 5;
return getalletje;
},
self.$('.geldinuit-button').click(function(){
self.screen_selector.show_popup('geldinuit',{
message: _t('Popup titel'),
comment: _t('getalletje'),
confirm: function(){
window.alert(getalletje);
},
});
});
另外,不应该:
comment: _t('getalletje'),
成为
comment: _t(getalletje),
更新
此外,在您的情况下,此声明没有用。我不知道您在提出问题时是否更改了代码,但在这种情况下只做:
self.$('.geldinuit-button').click(function(){
var getalletje = 5;
self.screen_selector.show_popup('geldinuit',{
message: _t('Popup titel'),
comment: _t('getalletje'),
confirm: function(){
window.alert(getalletje);
},
});
});
这是一个范围问题 - 您声明的变量仅在本地可用。要获得它的价值,您可以这样做:
confirm: function(){
window.alert( yourObject.get_name() );
},
其中 yourObject
是您为其定义 get_name
方法的对象。
当我在 javascript 中使用变量时,出现错误
ReferenceError: getalletje is not defined
,取决于我在哪里声明它。
这是我声明变量的地方:
get_name: function(){
var getalletje = 5;
return getalletje;
},
我尝试使用变量的地方:
self.$('.geldinuit-button').click(function(){
self.screen_selector.show_popup('geldinuit',{
message: _t('Popup titel'),
comment: _t('getalletje'),
confirm: function(){
window.alert(getalletje);
},
});
});
它给出了这样的错误。
但是:如果我将 var getalletje = 5;
放在 self.$('.geldinuit-button').click(function(){
的正上方,它会起作用。
我还需要做些什么吗?
为 Shomz 编辑:这是完整代码:
.............
self.set_smart_status(status.newValue);
});
this.$el.click(function(){
self.pos.connect_to_proxy();
});
},
});
module.PosWidget = module.PosBaseWidget.extend({
template: 'PosWidget',
init: function() {
this._super(arguments[0],{});
this.pos = new module.PosModel(this.session,{pos_widget:this});
this.pos_widget = this; //So that pos_widget's childs have pos_widget set automatically
.............................
............................
get_name: function(){
var getalletje = 5;
return getalletje;
},
...........................
...........................
self.$('.geldinuit-button').click(function(){
self.screen_selector.show_popup('geldinuit',{
message: _t('Popup titel'),
comment: _t('getalletje'),
confirm: function(){
window.alert( this.get_name() );
},
});
});
..........................
你不能在声明它的函数之外访问变量。如果你想以全局方式访问变量,你应该在 get_name 之外声明它。 在你的情况下,我不明白这一点,但以下应该有效:
var getalletje = 0;
get_name: function(){
getalletje = 5;
return getalletje;
},
self.$('.geldinuit-button').click(function(){
self.screen_selector.show_popup('geldinuit',{
message: _t('Popup titel'),
comment: _t('getalletje'),
confirm: function(){
window.alert(getalletje);
},
});
});
另外,不应该:
comment: _t('getalletje'),
成为
comment: _t(getalletje),
更新
此外,在您的情况下,此声明没有用。我不知道您在提出问题时是否更改了代码,但在这种情况下只做:
self.$('.geldinuit-button').click(function(){
var getalletje = 5;
self.screen_selector.show_popup('geldinuit',{
message: _t('Popup titel'),
comment: _t('getalletje'),
confirm: function(){
window.alert(getalletje);
},
});
});
这是一个范围问题 - 您声明的变量仅在本地可用。要获得它的价值,您可以这样做:
confirm: function(){
window.alert( yourObject.get_name() );
},
其中 yourObject
是您为其定义 get_name
方法的对象。