5
function validateHistoryId(input) {
    $.getJSON('request.php', {"action": "historyExists", "history_id" : input.value},
              function(data) {
                  console.log(input.value);
              }
    );
}

i use jQuery ajax call from javascript function. I tried the above code and it works, but I don't know will it cause any problems in the future.

I want to know, can ajax callback function see variables of it's parent function? and is it a bad practice to do so?

5
  • 2
    Yes it can see the variables of its parent functions and i don't think it will count as a bad practice because many a times data from the ajax calls are needed outside itself and we can use its parent's variable for the same. Commented Oct 19, 2012 at 5:53
  • It's ok, shouldn't be a problem. Commented Oct 19, 2012 at 5:54
  • those are the answers I wanted to hear :) thx ;) Commented Oct 19, 2012 at 5:59
  • You probably want to read this: developer.mozilla.org/en-US/docs/JavaScript/Guide/… and developer.mozilla.org/en-US/docs/JavaScript/Guide/… Commented Oct 19, 2012 at 6:01
  • Note that this is a standard JavaScript thing, with or without the jQuery library. Commented Oct 19, 2012 at 6:07

1 Answer 1

10

This is the JavaScript functional way to doing things. It's called closure: functions carry variable pointers from their current scope and from any other parent scope. So it's not only good practice, but this is the pattern you should normally follow instead of pushing around parameter objects, etc.

Please notice that the "this" reference is special, it is never closured (as opposed to any other references) and thus always point the global object in anonymous functions.

In fact a developer needs some time to fully employ the power of the closure feature - this is a basic example you just written. In more advanced (and not solely async) scenarios closure helps you to create "fire and forget" behavior, or can provide you with "private" variables that are not accessible from client code (useful for library development). Closure also help you isolate your code so that it will not mess with the global scope.

1) Example: how to create protected variables with closures. You are able to acccess two methods that can access "protectedVariable" but you are not able to access it yourself - so the control is guaranteed.

function protectedScope(initData) {
  var protectedVariable = initData;

  return {
    getter: function()  { return protectedVariable; }
    setter: function(v) { protectedVariable = v; }
  }
}

var methods = protectedScope(10);

console.log(methods.getter());

2) isolation: the following code will not garbage the global scope even with "global" function definitions

var API = (function() {
   var protectedVariable = 0;

   var myNotGlobalFunction() {
      return protectedVariable;
   }

   return myNotGlobalFunction;
})();
Sign up to request clarification or add additional context in comments.

1 Comment

wow, that's a really detailed explanation. I did learn many things from this post. Thanks a lot! ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.