0

Take a look at this example:

function aaa () {
    console.dir(this)
}

function bbb () {}

aaa.apply(undefined, [1,2,3]) // this in aaa is `window` object
aaa.apply(bbb, [1,2,3]) // this in aaa is `bbb` function

Why is this set to window in first apply case, even though I'm trying to force it to be undefined?

bodacydo
  • 75,521
  • 93
  • 229
  • 319
  • Because your function is in sloppy mode. – Bergi Oct 28 '15 at 02:04
  • Non-"strict mode". Add `"use strict";` before the `console.dir` and it will show `undefined`. – Bergi Oct 28 '15 at 02:05
  • Never heard of strict mode, thanks. I'll look it up. – bodacydo Oct 28 '15 at 02:06
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode – Bergi Oct 28 '15 at 02:07
  • See http://stackoverflow.com/q/13997247/1048572, http://stackoverflow.com/q/17150951/1048572 and http://stackoverflow.com/q/19804315/1048572 – Bergi Oct 28 '15 at 02:13
  • I've a follow up question now - Should I be worried about strict mode? I've been coding not knowing about it and my apps simply work. I've been calling `.apply(undefined)` for a long time and then using `windows` as `this`. I wonder if I should somehow now check if strict mode is on and then pass `windows` explicitly to apply in case the first value is undefined....... – bodacydo Oct 28 '15 at 02:13
  • 2
    @bodacydo - It is highly recommended that you read and learn about strict mode and the differences with non-strict mode and then start using it for all your code. strict mode will turn some coding mistakes into immediate errors so you see them right away and can fix them rather than attempting to cover them up. – jfriend00 Oct 28 '15 at 02:14
  • dont use `undefined` its a *variable* defined on `window`, it can be overridden or not available. – LJᛃ Oct 28 '15 at 03:16
  • @LJᛃ can you say that again? undefined is a variable defined on windows? what? – bodacydo Oct 28 '15 at 16:29
  • In older browsers `undefined` was a variable, so one could go and set `window.undefined = "I'm so not undefined bro"`, I just checked and turns out that this is not the case anymore in modern browsers Firefox 4+ etc. so not an issue anymore I guess. Its still not a keyword, so one could create a variable `var undefined = 123` within the scope of a function. I would advise to use `null` instead of `undefined`. – LJᛃ Oct 29 '15 at 02:39

2 Answers2

5

When not in strict mode and either null or undefined is passed as the first argument to .apply(), this will be set to the global object which is window in a browser.

In strict mode, it would set this to the actual value you pass.


In general non-strict mode tried to be tolerant of mistakes and even "fix" some mistakes automatically for you. This proved to sometimes be a problem because things that should have been immediate coding errors were "covered up" by the system. strict mode was invented for a number of reasons and one of the reasons was to stop hiding coding errors.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Is `undefined` the same as no value? I'm passing it undefined as a value? or is `undefined` not a value? – bodacydo Oct 28 '15 at 02:06
  • I'll have to look up what is strict mode. – bodacydo Oct 28 '15 at 02:06
  • 1
    @bodacydo - With some brief testing in Chrome in non-strict mode, "no value" in this case is `null` or `undefined`. Those values are converted to `window`. Others are used. – jfriend00 Oct 28 '15 at 02:09
  • @bodacydo: Yes, `undefined` is just as good as "no value", when you talk of function calls like `fn.apply()`. In sloppy mode, the `this` value always is an object, so primitive strings, numbers and booleans are wrapped; and instead of `null` or `undefined` the global object is used. – Bergi Oct 28 '15 at 02:16
  • Very cool. Learning something new every day. What a wonderful experience. – bodacydo Oct 28 '15 at 02:17
1

Because that's how function calls work if you're not in strict mode. Without strict mode, a function's this reference always refers to an object, and it'll be the global object (i.e. window) if no other object was given.

Wyzard
  • 33,849
  • 3
  • 67
  • 87