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")
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
so if that code is executed as a callback within another function that is in strict mode,
thiswill not refer to the globalwindow, but rather it will beundefined.