gpt4 book ai didi

Javascript 变量作用域

转载 作者:行者123 更新时间:2023-11-30 06:05:26 24 4
gpt4 key购买 nike

我可以在内部调用 selectCompanyJump(this) 而不是从 App.site.profile 调用它吗?

我可以不做 App.site.profile.selectStateJump(this); 而不是像 parent.selectStateJump(this); 那样不重新分配 this 在 .change() 调用之外?

$(document).ready(function () {
App.site = function () {
return {
init: function () {
this.profile.init();
},
profile: function () {
var profile;

return {
init: function () {
profile = $('div#profile');

$('select[name="company_id"]', profile).change(function () {
App.site.profile.selectCompanyJump(this);
});

$('select[name="state_id"]', profile).change(function () {
App.site.profile.selectStateJump(this);
});
},
selectCompanyJump: function (select) {
$(select.parent()).submit();
},
selectStateJump: function (select) {
$(select.parent()).submit();
}
}
}()
}
}();

App.site.init();
});

最佳答案

您可以将所需的“this”范围作为 change() 函数定义之外的另一个变量进行引用:

     profile: function () {
var profile;

return {
init: function () {
profile = $('div#profile');
var self = this;

$('select[name="company_id"]', profile).change(function () {
self.selectCompanyJump(this);
});

$('select[name="state_id"]', profile).change(function () {
self.selectStateJump(this);
});
},
selectCompanyJump: function (select) {
$(select.parent()).submit();
},
selectStateJump: function (select) {
$(select.parent()).submit();
}
}
}()

关于Javascript 变量作用域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5251315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com