0

Lets say I have an ajax call like this:

$.ajax({
     ...
     success : someFn
})

On the code review, they asked about what environment does someFn have access to? They were more worried about, what all variables, scopes does someFn have access to and how I'm going to manage the changes that are done in someFn.

For example if :

someFn : function()
{
    //1. changes some object value
    //2. Works based on some scope variables
}

Now the question is:

  1. How I can send the scope variables to someFn and reason about them?
  2. If the scope variables is an reference to another object, how should I handle those changes within someFn.

Thanks in advance

Moppo
  • 18,797
  • 5
  • 65
  • 64
batman
  • 3,565
  • 5
  • 20
  • 41

2 Answers2

3

The variables in scope for someFn are determined by where someFn is defined, not where you've used it. Using it in your ajax call does nothing to grant it access to anything it doesn't already have access to.

The variables in scope for a function are the ones in scope where it's created, which are: The locals for the context where it's created, the context containing that context, the context containing that context, and so on through the global context.

So:

function foo() {
    var a;

    function bar() {
        var b;
    }

    return bar;
}

The code in foo has access to all global variables, and also its local variable a. bar has access to all global variables, the a variable created by the call to foo that created bar, and (of course) its local variable b.

More (on my blog): Closures are not complicated

If you have context information that you need to pass to someFn from your ajax context, you'll need to define a new function that passes that information to someFn as arguments. (Either explicitly, or using Function#bind, which creates a new function which, when called, calls the original with a specific this value and any other arguments you give bind.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thanks for the answer. You would prefer `Function#bind` or passing as an argument? – batman Jun 04 '15 at 11:39
  • @batman: You'd be using arguments either way, it's just a matter of how. (Unless you're using `this` within `someFn` and manipulate that; but `this`, for normal functions, basically *is* a function argument.) The decision of whether to use an explicit function or `bind` relates to several things: 1. Do you need a copy of the value as of when you call `bind`, or do you want to use the value as of when the callback occurs? (If the latter, you can't use `bind`.) Do you need to mix things in with arguments you receive? (Then you can't use `bind`, which can only insert args at the beginning.) Etc. – T.J. Crowder Jun 04 '15 at 11:43
  • Crowder: Thanks for your valuable answer. In your answer you have mentioned : `you'll need to define a new function that passes that information to someFn as arguments.` I have a question here, why can't we directly pass the arguments to `someFn`, why to create a new fn and that does call `someFn` with arguments? – batman Jun 04 '15 at 12:01
  • @batman: Because in `success: someFn`, you're not **calling** `someFn`, you're referring to it. The ajax stuff will call it *later*. So if you have something in scope you want to pass it, you'd do `success: function(result) { someFn(result, something, more); }` or of course `success: someFn.bind(null, something, more)` (note that in the second case, `someFn` will get `something`, then `more`, then the result from the ajax call (the ones you give `bind` come first). – T.J. Crowder Jun 04 '15 at 12:28
0
  1. pass the scope variables to someFn by

    success : function(){
          someFn(scope_var1, scope_var2);
    }
    
    • you could as well use the bind method to preserve the this context
  2. incase you make changes to that object take into account that it would affect anyone using it. You could either clone your object $.extend(true,clonedObj,someObj)
hamecoded
  • 1,065
  • 7
  • 5