What does "this" mean in this exponential module module

var abc=(function(){

     var self=this;

     return {
        self:self
     }             


})();

When abc.selfI execute, I get undefinedwhat happens in this context this.

+4
source share
3 answers

What can you simplify for explanation in

(function(){ console.log(this) })();

Your expression (in the first set of brackets) defines the function. Then you call this function without context ( this). This design is called IIFE . Since you are not going through the context, the behavior depends on whether it is called in strict mode or not:

  • In non-standard mode, you will have a global object ( windowin the browser, globalin node).

  • , undefined.

undefined, , . , "use strict"; .

, , ,

(function(){ console.log(this) }).call(someobject);
+4

this . this window, , abc.self, :

this.self = this;

:

var xyz = new abc();
xyz.self

, abc , , .

+1

, " " this window

console.log(abc.self);

Window {top: Window, window: Window, ...}
+1

All Articles