variable scope in AJAX calls

一个人想着一个人 提交于 2019-12-24 05:08:05

问题


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);
  }
});

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

How is this possible?


回答1:


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/



来源:https://stackoverflow.com/questions/4146144/variable-scope-in-ajax-calls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!