0

Imagine you are doing the following inside a javascript function called Fetch.

function Fetch(context)
{
  var request = $.ajax({...});
  request.done(function(response)
  {
    // it looks like context is visible here and in Scope.
    //
  });

}

Can you explain why context is visible inside the callback function.?

Sam
  • 875
  • 10
  • 22
  • 2
    context is visible for everything inside the Fetch method. – Hanlet Escaño Sep 10 '13 at 16:57
  • Because it's declared in the parent of your done function. Any vars declared in the Fetch function will be available to any functions used in it. – Vlad Sep 10 '13 at 16:57
  • 6
    Because that's how closures work. Every function has access to the variables defined in the same or in a higher scope. – Felix Kling Sep 10 '13 at 16:58

1 Answers1

1

context is local to Fetch. request is declared inside of Fetch, therefore context is available inside request

tymeJV
  • 103,943
  • 14
  • 161
  • 157