1

My code is like this:

im.size((function(a, b) {
  console.log(b);
  console.log(im);
})(im));

The Object im has a function size, which expects a callback function. It passes the parameters a and b to the callback function. However I need to have the object im to be available within the callback. Therefore I pass it from the outside as an argument, however this "overwrites" the arguments a and b passed to the callback.

The output is:

undefined 
[My Object]

If I do:

im.size(function(a, b) {
  console.log(b);
  console.log(im);
});

The output is:

17
[My object] // edited: was undefined before

How can I pass the im object to be available in the scope of the callback as well as getting the variables passed to the callback? A bit of explanation would also be nice.

Edit: Actually the outer scope is accessible in my callback example. Is this also true for asynchronous callbacks and WHY is the outer scope accessible this way?

Blackbam
  • 17,496
  • 26
  • 97
  • 150
  • Possible duplicate of [How to access the correct \`this\` inside a callback?](https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback) – PM 77-1 Aug 17 '18 at 14:46
  • 2
    The second example should work too, giving that `im` is defined in that scope (deduced from the first example). – ibrahim mahrir Aug 17 '18 at 14:56
  • Can you post the complete code please, especially the place where you declare and initialise `im` (as well as everything else you do with that variable)? – Bergi Aug 17 '18 at 15:04
  • You don't need to "pass" anything to a closure. The variables simply stay in scope. – Bergi Aug 17 '18 at 15:05
  • 1
    @PM77-1 No, it has nothing to do with that. There's no mention of `this` in the question here. – Bergi Aug 17 '18 at 15:06
  • Also the first example is not passing a callback to `size`, it passes the result of the IIFE (which is `undefined` in this case). – ibrahim mahrir Aug 17 '18 at 15:07
  • @ibrahimmahrir You are right actually dunno which type cause the wrong output before!! Sorry for that. Still I wonder WHY im is in the scope of the callback? Is this always true even for asynchronous callbacks? – Blackbam Aug 17 '18 at 15:07
  • 1
    If a variable is accessible at the scope a function is defined in, it will be accessible within that function too. You could have done `im.size(im);` to pass `im` to size (for example), so if you do `im.size(function() {})`, `im` will be accessible to that function as well. – ibrahim mahrir Aug 17 '18 at 15:11
  • Thanks for your help. – Blackbam Aug 17 '18 at 15:12
  • You're welcome! – ibrahim mahrir Aug 17 '18 at 15:12
  • 2
    "*Is this also true for asynchronous callbacks*" - yes. "*WHY is the outer scope accessible this way?*" - that's just how it works - it's lexically in scope, so it is accessible. Other languages require you to explicitly state which variables you want to close over, JS doesn't. – Bergi Aug 17 '18 at 15:14

1 Answers1

1

The second example should work too, giving that im is defined in that scope (deduced from the first example).

If a variable is accessible at the scope a function is defined in, it will be accessible within that function too. You could have done im.size(im); to pass im to size (for example), so if you do im.size(function() {}), im will be accessible to that function as well.

Is this also true for asynchronous callbacks?

Yes.

WHY is the outer scope accessible this way?

That's just how it works - it's lexically in scope, so it is accessible. Other languages require you to explicitly state which variables you want to close over, JS doesn't.

Blackbam
  • 17,496
  • 26
  • 97
  • 150