当我在 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() );
},
});
});
..........................
最佳答案
这是一个范围问题 - 您声明的变量仅在本地可用。要获取其值,您可以执行以下操作:
confirm: function(){
window.alert( yourObject.get_name() );
},
其中 yourObject
是您为其定义 get_name
方法的对象。
关于JavaScript:变量未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32011343/