Skip to content Skip to sidebar Skip to footer

Javascript Global Module Or Global Variable

I couldn't come up with a better question title, sorry. My question is, in he.js as per https://github.com/mathiasbynens/he/blob/master/he.js , they are using the following code: /

Solution 1:

Let's simplify:

;(function(root) {
    ...
}(this));

So, we have a function that takes an argument called root. This function is being immediately invoked (http://benalman.com/news/2010/11/immediately-invoked-function-expression/), and we are passing the value this to it.

In this context, this is your global object. If you are using a browser, your global object will be window.

So, if you are indeed using a browser:

 root.he = he;

is actually the same as:

window.he = he;

And note, we don't necessarily need to be using a browser, thanks to platforms like Node there are now other contexts where the this global object is something other than window. That's why the other doesn't specify window explicitly and goes through this particular exercise.


Post a Comment for "Javascript Global Module Or Global Variable"