JS & EXT JS:函数作用域内的函数误解
我在使用以下代码时遇到了一些问题:
Ext.define('...controller...', {
extend: 'Ext.app.Controller',
init: function() {
...
},
crearLoginWindow:function(){
var loginWindow = Ext.create('Proto1.view.adminbd.LoginWindowBDView', {
itemId: 'loginwindow',
autoShow: true,
modal: true
});
Ext.ComponentQuery.query('#loginwindow > button[text="Cancelar"]')[0].on('click', function(){loginWindow.close()});
//Cant call 'enviarLogin' function if its inside 'crearLoginWindow'
Ext.ComponentQuery.query('#loginwindow > button[text="Conectar"]')[0].on('click', this.enviarLogin, this);
var enviarLogin = function(){
console.log('login')
}
}
});
我希望能够在“crearLoginWindow”函数中调用“enviarLogin”,但它会引发引用错误。如果该函数放置在“crearLoginWindow”之外,它将起作用。
这两行是麻烦的根源:
Ext.ComponentQuery.query('#loginwindow > button[text="Conectar"]')[0].on('click', this.enviarLogin, this);
var enviarLogin = function(){
console.log('login')
}
我尝试了不同的范围变体,例如;
Ext.ComponentQuery.query('#loginwindow > button[text="Conectar"]')[0].on('click', this.crearLoginWindow.enviarLogin);
this.enviarLogin = function(){
console.log('login')
}
这与我认为我对范围的理解以及指定函数所在位置的需要是有道理的。
我很感激一个解决方案,因为这个问题使我的代码非常混乱!
im having some trouble with the following code:
Ext.define('...controller...', {
extend: 'Ext.app.Controller',
init: function() {
...
},
crearLoginWindow:function(){
var loginWindow = Ext.create('Proto1.view.adminbd.LoginWindowBDView', {
itemId: 'loginwindow',
autoShow: true,
modal: true
});
Ext.ComponentQuery.query('#loginwindow > button[text="Cancelar"]')[0].on('click', function(){loginWindow.close()});
//Cant call 'enviarLogin' function if its inside 'crearLoginWindow'
Ext.ComponentQuery.query('#loginwindow > button[text="Conectar"]')[0].on('click', this.enviarLogin, this);
var enviarLogin = function(){
console.log('login')
}
}
});
I want to be able to call 'enviarLogin' inside 'crearLoginWindow' function but it throws a reference error. If the function is placed outside 'crearLoginWindow' it will work.
Being these two lines the source of trouble:
Ext.ComponentQuery.query('#loginwindow > button[text="Conectar"]')[0].on('click', this.enviarLogin, this);
var enviarLogin = function(){
console.log('login')
}
I've tried different scope variants such as;
Ext.ComponentQuery.query('#loginwindow > button[text="Conectar"]')[0].on('click', this.crearLoginWindow.enviarLogin);
this.enviarLogin = function(){
console.log('login')
}
Which makes sense with what i think i understand about scope and the need to specify the place where the function resides to execute it.
Id appreciate a solution because this problem makes my code very messy!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单地在另一个函数中定义一个函数不会将其附加到任何对象或实例,因此
this.enviarLogin
将不起作用。这里有两个基本选项:只需定义您正在执行的内部函数(将其移至处理程序分配之上),并直接按名称引用它:
将 enviarLogin 定义为方法,与定义
crearLoginWindow
的方式相同:如果不需要引用,第一个版本可能会更好
enviarLogin
在crearLoginWindow
方法之外。如果您确实需要在其他地方引用enviarLogin
,则第二种更好,并且它使得在enviarLogin
中使用this
更加清晰。Simply defining a function within another function doesn't attach it to any object or instance, so
this.enviarLogin
won't work. There are two basic options here:Just define the inner function as you're doing (moving it up above your handler assignment), and reference it directly by name:
Define
enviarLogin
as a method, the same way you are definingcrearLoginWindow
:The first version may be better if you don't need to reference
enviarLogin
outside of thecrearLoginWindow
method. The second is better if you do need to referenceenviarLogin
elsewhere, and it makes usingthis
inenviarLogin
clearer.