JavaScript 变量作用域

发布于 2024-10-08 10:34:42 字数 387 浏览 4 评论 0原文

我已经在函数内声明了 var1 。然而,当我尝试在inner_function2中分配一个值时,似乎创建了另一个var1而不是引用第一个。

var func1 = function(response) {

    var var1;

    function inner_function2(param) {

        Ext.Ajax.request({
            url:'',
            params: param,
            success:function(result, request) { 
                var1 = a;
                }
            });
    }   

}()

I have declared var1 inside the function. However, when i try to assign a value in inner_function2, it seems another var1 is create instead of referencing to the first one.

var func1 = function(response) {

    var var1;

    function inner_function2(param) {

        Ext.Ajax.request({
            url:'',
            params: param,
            success:function(result, request) { 
                var1 = a;
                }
            });
    }   

}()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

心凉 2024-10-15 10:34:42

AJAX 请求返回后,会调用 Ext.Ajax.requestsuccess 回调。这意味着调用它的函数(func1()inner_function2() 都已返回,并且它们的作用域已被删除。

让我们尝试显示一系列事件

  1. < code>func1() 被调用
  2. 局部变量 var1 被声明
  3. inner_function2() 被调用(我假设你确实在某个地方调用了它)
  4. Ext调用 .Ajax.request(),并声明 success 回调
  5. inner_function2() 结束 - AJAX 请求在后台运行
  6. func1() 结束,局部变量 var1 被删除(尽管它在回调作用域中仍然可用) - AJAX 请求在后台运行
  7. AJAX 请求完成 - success 回调由于没有局部变量var1,因此创建了一个全局变量并为其分配了值a(请参阅下面的注释,了解为什么要删除它) ). 局部变量 var1 在回调的作用域中仍然可用,但是一旦回调结束,该变量就不再在任何地方可用,因此您无法使用它。

请记住,AJAX 中的“A”代表“异步”。

success callback of Ext.Ajax.request is called after AJAX request has been returned. This means, the function that called it (both func1() and inner_function2() have already returned, and their scope was deleted.

Let's try to show a sequence of events

  1. func1() is called
  2. local variable var1 is declared
  3. inner_function2() is called (I'm assuming you do call it somewhere)
  4. Ext.Ajax.request() is called, with success callback declared
  5. inner_function2() ends - the AJAX request is running in the background
  6. func1() ends, local variable var1 is deleted (it still is available in callback scope though)- the AJAX request is running in the background
  7. AJAX request finishes - success callback is called. Since there is no local variable var1 a global variable is created and assigned value of a (see the comments below why this is deleted). Local variable var1 is still available in callback's scope, but once the callback ends, the variable is no longer available anywhere, so you can't use it.

Remember that 'A' in AJAX stands for 'Asynchronous'.

野の 2024-10-15 10:34:42

我建议您在代码中明确指定范围。你可以这样做:

var func1 = function(response) {
  var var1;
  var outer_scope = this;

  function inner_func2(param) {
    Ext.Ajax.request({
      url: '',
      params: param,
      scope: outer_scope,
      success: function(result, request) {
        var1 = a;
      }
    });
  }
}()

希望它有效!

I suggest you specify the scope explicitly in your code. you can do like:

var func1 = function(response) {
  var var1;
  var outer_scope = this;

  function inner_func2(param) {
    Ext.Ajax.request({
      url: '',
      params: param,
      scope: outer_scope,
      success: function(result, request) {
        var1 = a;
      }
    });
  }
}()

Hope it works!

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