4

i'm trying to encapsolate my code inside an immediate function that later on will be accessed via the global variable x and act like a "module".

code:

var x = (function () {

    console.log(x); // undefined
    console.log(this); // undefined

})();

But I don't understand why I cant use this to refer to the function itself.

EDIT:

the immediate function is inside another function in strict mode ("use strict")

gdoron
  • 147,333
  • 58
  • 291
  • 367
Shlomi Komemi
  • 5,445
  • 3
  • 28
  • 41

5 Answers5

1

there's a funny thing that happens when a function is executed within a function, or handed as a callback to another function that is handled in strict mode

here's a demo, and watch the console

function foo(){
    'use strict';

    (function(){
        //undefined in strict mode
        console.log('in foo, this is: '+this);  
    }());

}

function bar(){

    (function(){
        //DOMWindow when NOT in strict mode
        console.log('in bar, this is: '+this); 
    }());

}

foo();
bar();​

so if that code is executed as a callback within another function that is in strict mode, this will not refer to the global window, but rather it will be undefined.

Joseph
  • 117,725
  • 30
  • 181
  • 234
  • thanks, do u know what is the reason for this strange behavior ? – Shlomi Komemi Apr 12 '12 at 09:40
  • it's explained in [this section](https://developer.mozilla.org/en/JavaScript/Strict_mode#.22Securing.22_JavaScript) of the article i provided. a [similar answer](http://stackoverflow.com/a/1335881/575527) also addressed why, mainly so that code within functions don't gain access to the global object. – Joseph Apr 12 '12 at 09:50
0

You can not use "this" to refer to the function itself. "this" points to the instance of an object inside the object.

Dima
  • 1,717
  • 15
  • 34
0

this is the function owner. not the function itself.

and by the way, the output you reported is incorect:

console.log(this); // DOMWindow

DEMO

That's because the owner of the immediate function is the Global-Object - window.

As commented, this doesn't always must be the DOMWindow object, but still it can never be undefined!

gdoron
  • 147,333
  • 58
  • 291
  • 367
0

A self executing function (immediate function) only provide a context of execution for the time it is run. x will always be undefined because in your code it is assigned the result of the immediate function and as it returns nothing x is undefined.

this shouldn't be undefined tho, in an anonymous function it would refer to the DOMWindow object, the undefined you are seeing is only the result of the anonymous function.

If you want a "module" or class like behaviour then just

var x = function() {
console.log(x, this);
};

would do as it would make x a constructor you can then extend the object with properties you want to inherit using the prototype property.

x.prototype.foo = function() {

}
GillesC
  • 10,647
  • 3
  • 40
  • 55
0

You should not use this inside immediate anon func, it's meaningless. this suggests later use of created object, and immediate functions have opposite purpose - to destroy it's namespase right after execution. In your example this shall refer to global object, window.

oxfn
  • 6,590
  • 2
  • 26
  • 34