1

I'm playing around with scopes in JavaScript and I was curious about something I ran across when calling a function from an array. In the below example I work with three different scopes. One bound to an Object called foobar, one bound to window and then a third one which actually refers to the function itself. I'm just curious why the function is scoped to itself and not to the global window object. Is it because Array access is a function call itself so the stored function is in a local scope?

var foobar = {
  doWork: function() {
      console.log('doing some work...');
      console.log(this);
  }
}

foobar.doWork(); // `this` will refer to foobar

var doWorkClone = foobar.doWork;
doWorkClone(); // `this` will refer to window

var workClones = [];
workClones.push(foobar.doWork);
workClones[0](); // `this` will refer to the doWork function itself
13
  • 2
    That value of this has nothing to do with scope Commented Aug 22, 2012 at 16:38
  • Functions are never really "bound" to anything in JavaScript. The value of this is determined upon each function call based on the situation. Commented Aug 22, 2012 at 16:38
  • 2
    "this will refer to the doWork function itself" — No, it won't. Note the [ and ] in the output. It refers to workClones Commented Aug 22, 2012 at 16:39
  • 1
    @Humberto: Strictly speaking that's just a trick. The function that .bind returns calls the original function with a predefined this - but neither of the two functions have a this value bound, technically. Commented Aug 22, 2012 at 16:42
  • 1
    @Humberto oh yes, you're definitely correct about the behavior - really we're just being picky about the terminology :-) Commented Aug 22, 2012 at 16:48

2 Answers 2

4

They behave the same way. In a.b(), the function a.b is called with this set to a.

foobar.doWork();  // function is `foobar.doWork`, `this` is `foobar`
workClones[0]();  // function is `workClones[0]`, `this` is `workClones`

This is because the . and [] notation are functionally the same thing. It does not matter which one you use, nor does it matter whether you're dealing with an array or not.

Sign up to request clarification or add additional context in comments.

Comments

0

to understand this, litteraly, it might be helpfull to see how the js works under the hood.

when you write f(args), js will execute f.call(this, args). Hence this always refers to where you call the function from.

In your case:

foobar.doWork()   --> foobar
doWorkClone()     --> window or wrapping expression
workClones[0]()   --> "0" is in workClones, so workClones

3 Comments

The examples are correct but I don't fully agree with your this reasoning. Surely foobar.doWork.call(this) is different.
f(args) is not like f.call(this, args) at all. Or were you referring to a special case? In general it's completely wrong.
Of course "this" is different when you type it down (as you overwrite the "this" passed by default). We can argue than "this" is actually the parent object, but I won't as your your explanation is already way more clear.

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.