Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is "this" in an anonymous function undefined when using strict?

Why is this in an anonymous function undefined when using javascript in strict mode? I understand why this could make sense, but I couldn't find any concrete answer.

Example:

(function () {     "use strict";      this.foo = "bar"; // *this* is undefined, why? }()); 

Test in a fiddle: http://jsfiddle.net/Pyr5g/1/ Check out the logger (firebug).

like image 972
T. Junghans Avatar asked Mar 22 '12 12:03

T. Junghans


People also ask

Why is this undefined in strict mode?

In strict mode, it is now undefined . When a function was called with call or apply , if the value was a primitive value, this one was boxed into an object (or the global object for undefined and null ). In strict mode, the value is passed directly without conversion or replacement.

Why is this undefined in function JavaScript?

This is a common JavaScript error that happens when you try to call a function before it is defined. You get this error when you try to execute a function that is uninitialized or improperly initialized . It means that the expression did not return a function object.

How can this be undefined?

A variable that has not been assigned a value is of type undefined . A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned. Let's try to understand by a simple example.

How do you define an anonymous function?

An anonymous function is a function that is not stored in a program file, but is associated with a variable whose data type is function_handle . Anonymous functions can accept multiple inputs and return one output. They can contain only a single executable statement.


2 Answers

It's because, until ECMAscript 262 edition 5, there was a big confusion if people who where using the constructor pattern, forgot to use the new keyword. If you forgot to use new when calling a constructor function in ES3, this referenced the global object (window in a browser) and you would clobber the global object with variables.

That was terrible behavior and so people at ECMA decided, just to set this to undefined.

Example:

function myConstructor() {     this.a = 'foo';     this.b = 'bar'; }  myInstance     = new myConstructor(); // all cool, all fine. a and b were created in a new local object myBadInstance  = myConstructor(); // oh my gosh, we just created a, and b on the window object 

The last line would throw an error in ES5 strict

"TypeError: this is undefined" 

(which is a much better behavior)

like image 65
jAndy Avatar answered Oct 07 '22 17:10

jAndy


There is a mechanism called "boxing" which wraps or change the this object before entering the context of the called function. In your case, the value of this should be undefined because you are not calling the function as a method of an object. If non strict mode, in this case, this is replaced by the window object. In strict mode it's always unchanged, that's why it's undefined here.

You can find more information at
https://developer.mozilla.org/en/JavaScript/Strict_mode

like image 44
Samuel Rossille Avatar answered Oct 07 '22 18:10

Samuel Rossille