需要有关 Javascript 变量作用域的帮助
我有以下 Javascript 函数,它应该返回数据库中的组数组。它使用 $.getJSON()
方法调用 get_groups.php
来实际从数据库读取数据。
function get_groups() {
var groups = [];
$.getJSON('get_groups.php', function(response) {
for (var i in response) {
groups.push(response[i]);
}
}
return groups;
}
不幸的是,这个函数无法按预期工作,因为groups.push(response[i]); 不会填充 var groups = [];
(据我了解,它填充了其他一些 groups
数组,可能是全局数组)。
假设我不想有一个全局groups
变量,你会如何解决这个问题?
I have the following Javascript function that should return an array of groups that are in database. It uses $.getJSON()
method to call get_groups.php
which actually reads from the database.
function get_groups() {
var groups = [];
$.getJSON('get_groups.php', function(response) {
for (var i in response) {
groups.push(response[i]);
}
}
return groups;
}
Unfortunately, this function does not work as expected because groups.push(response[i]);
does not fill the var groups = [];
(as I understand it fills some other groups
array, probably the global one).
Assuming that I don't want to have a global groups
variable, how would you solve this problem ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这实际上不是范围问题,而是
$.getJSON()
是异步的,这意味着这部分在您返回后运行:您需要在异步请求的回调中调用需要此数据的任何函数,因此它会在数据可用时运行,如下所示:
目前您的分组数组正在被填充,只是不是在您需要的时候填充。如果您绝对必须返回此值(我强烈建议按照预期方式使用异步模型),您可以使用完整的
$.ajax()
版本并设置async:false
。再说一遍……如果可能的话,不要走这条路,一旦数据可用,就坚持调用需要数据的任何函数,因为async: false
将锁定用户的浏览器。It's not a scope issue really, it's the fact that
$.getJSON()
is asynchronous, meaning that this part runs after you return:You need to call whatever function needs this data in the callback of the asynchronous request, so it runs when the data's available, like this:
Currently you groups array is getting populated, just not when you need it to. If you absolutely have to return this (I strongly recommend using the asynchronous model the way it was intended) you can use the full
$.ajax()
version and setasync:false
. Again...don't go that route if possible, stick to calling whatever function needs the data once it's available, asasync: false
will lock up the user's browser.除非您确实有一个名为
groups
的全局变量(实际上这不是最好的主意),否则您正在与“本地”groups
变量交谈。由于 EMCA-/Javascript 确实具有函数作用域,并且您在那里使用闭包,因此您确实可以访问该变量。所以这里的问题,不是范围。
因此,即使全局变量具有完全相同的名称,这种所谓的词法作用域也将保证您可以访问局部变量。
实际的问题是,
return groups
在$.getJSON()
完成之前执行。由于它创建了一个 ajax 请求,因此它异步运行。您应该自己使用
回调
来处理数据:Unless you really have a global variable with the name
groups
(which would be not the best idea actually), you are talking to your "local"groups
variable.Since EMCA-/Javascript does have function scope and you are using a closure there, you do have access to that variable. So the problem here, is not the scope.
So even with a global variable with the exact same name, the such called lexical scope will guarantee you the access to your local variable.
The actuall problem is, that
return groups
is executed before$.getJSON()
does complete. Since it creates an ajax request, it run asynch.You should use a
callback
yourself to process the data:"$.getJSON('get_groups.php', function(response) {" 是一个回调函数。groups
-Array 中的更改会在回调被欺骗后以及返回组后生效。get_groups
您无法直接返回回调函数的修改。
"$.getJSON('get_groups.php', function(response) {" is an Callback-Function.
The changes in the groups-Array are affect after the callback is trickerd and also after you return groups.
you could not return the modifies of the callback-function directly.