需要有关 Javascript 变量作用域的帮助

发布于 2024-09-14 20:13:20 字数 538 浏览 7 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

通知家属抬走 2024-09-21 20:13:20

这实际上不是范围问题,而是 $.getJSON() 是异步的,这意味着这部分在您返回后运行:

for (var i in response) {
  groups.push(response[i]);
}

您需要在异步请求的回调中调用需要此数据的任何函数,因此它会在数据可用时运行,如下所示:

$.getJSON('get_groups.php', function(response) {
    var groups = [];
    for (var i in response) {
        groups.push(response[i]);
    }
    doSomethingThatNeedsGroups(groups);
});

目前您的分组数组正在被填充,只是不是在您需要的时候填充。如果您绝对必须返回此值(我强烈建议按照预期方式使用异步模型),您可以使用完整的 $.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:

for (var i in response) {
  groups.push(response[i]);
}

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:

$.getJSON('get_groups.php', function(response) {
    var groups = [];
    for (var i in response) {
        groups.push(response[i]);
    }
    doSomethingThatNeedsGroups(groups);
});

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 set async:false. Again...don't go that route if possible, stick to calling whatever function needs the data once it's available, as async: false will lock up the user's browser.

多孤肩上扛 2024-09-21 20:13:20

除非您确实有一个名为 groups 的全局变量(实际上这不是最好的主意),否则您正在与“本地”groups 变量交谈。

由于 EMCA-/Javascript 确实具有函数作用域,并且您在那里使用闭包,因此您确实可以访问该变量。所以这里的问题,不是范围。
因此,即使全局变量具有完全相同的名称,这种所谓的词法作用域也将保证您可以访问局部变量。

实际的问题是,return groups$.getJSON() 完成之前执行。由于它创建了一个 ajax 请求,因此它异步运行。

您应该自己使用回调来处理数据:

function get_groups(cb) {
   var groups = [];

   $.getJSON('get_groups.php', function(response) {
       for (var i in response) {
           groups.push(response[i]);
       }
       cb.apply(null, [groups]);
   }
}

get_groups(function(groups){
   // do something with groups array
});

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:

function get_groups(cb) {
   var groups = [];

   $.getJSON('get_groups.php', function(response) {
       for (var i in response) {
           groups.push(response[i]);
       }
       cb.apply(null, [groups]);
   }
}

get_groups(function(groups){
   // do something with groups array
});
节枝 2024-09-21 20:13:20

"$.getJSON('get_groups.php', function(response) {" 是一个回调函数。groups

-Array 中的更改会在回调被欺骗后以及返回组后生效。get_groups

  1. 输入
  2. 请求 url 和注册回调
  3. 返回组
  4. “get_groups”结束
  5. 触发回调
  6. 进入任意回调函数
  7. 修改组数组
  8. 任意回调函数结束

您无法直接返回回调函数的修改。

"$.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.

  1. get_groups enterd
  2. request the url and register a callback
  3. return groups
  4. end of "get_groups"
  5. trigger callback
  6. anynomous callback-function enterd
  7. modifie groups-array
  8. end of anynomous callback-function

you could not return the modifies of the callback-function directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文