在AJAX调用变量的作用域 [英] variable scope in AJAX calls

查看:1288
本文介绍了在AJAX调用变量的作用域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个问题,我一直在问自己是怎么可能的JavaScript有仍处于一个AJAX请求回调函数的引用,当变量是在isssues AJAX调用的函数声明。下面的例子

one question I always ask myself is how is it possible that javascript has still a reference in a callback function of a AJAX request when the variable was declared in the function which isssues the AJAX call. Here an example

var loadMask = {name:"test"};

form.submit({
  url: 'request.php',
  timeout : 180000,
  success: function(the_form, action_object)
  {    
    console.log(loadMask);
  }
});

尽管loadMask是成功的功能它仍然是可见的外部声明(和定义)的事实中。

despite the fact that loadMask was declared outside of the success function it is still visible (and defined) inside.

这怎么可能?

推荐答案

这是有可能使用一种叫做关闭。有许多资源,这样的:

This is possible using something called closures. There are many resources for this:

下面是来自谷歌的几个:

Here's a few from a google:

http://www.webreference.com/programming/javascript/rg36/

http://jibbering.com/faq/notes/closures/

这篇关于在AJAX调用变量的作用域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆