Monday, July 29, 2019

scope - What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”?



I have been reading a lot of Javascript lately and I have been noticing that the whole file is wrapped like the following in the .js files to be imported.



(function() {
...
code
...
})();



What is the reason for doing this rather than a simple set of constructor functions?


Answer



It's usually to namespace (see later) and control the visibility of member functions and/or variables. Think of it like an object definition. jQuery plugins are usually written like this.



In Javascript, you can nest functions. So, the following is legal:



function outerFunction() {
function innerFunction() {

// code
}
}


Now you can call outerFunction(), but the visiblity of innerFunction() is limited to the scope of outerFunction(), meaning it is private to outerFunction(). It basically follows the same principle as variables in Javascript:



var globalVariable;

function someFunction() {

var localVariable;
}


Correspondingly:



function globalFunction() {

var localFunction1 = function() {
//I'm anonymous! But localFunction1 is a reference to me!

};

function localFunction2() {
//I'm named!
}
}


In the above scenario, you can call globalFunction() from anywhere, but you cannot call localFunction1 or localFunction2.




What you're doing when you write (function() { ... code ... })(), is you're making the code inside a function literal (meaning the whole "object" is actually a function). After that, you're self-invoking the function (the final ()). So the major advantage of this as I mentioned before, is that you can have private methods/functions and properties:



(function() {
var private_var;

function private_function() {
//code
}
})()



In the first example, globalFunction() was the public function that could be called to access the public functionality, but in the above example how do you call it? Here the self-invoking function makes the code automatically run at start up. Just like you can add initMyStuff(); to the top of any .js file and it will automatically run as part of the global scope, this self-invoking function will also automatically run, although since it's an unnamed function it cannot be called multiple times like initMyStuff() could be.



The neat thing is that you can also define things inside and expose it to the outside world so (an example of namespacing so you can basically create your own library/plugin):



var myPlugin = (function() {
var private_var;

function private_function() {
}


return {
public_function1: function() {
},
public_function2: function() {
}
}
})()



Now you can call myPlugin.public_function1(), but you cannot access private_function()! So pretty similar to a class definition. To understand this better, I recommend the following links for some further reading:





EDIT



I forgot to mention. In that final (), you can pass anything you want inside. For example, when you create jQuery plugins, you pass in jQuery or $ like so:



(function(jQ) { ... code ... })(jQuery) 



So what you're doing here is defining a function that takes in one parameter (called jQ, a local variable, and known only to that function). Then you're self-invoking the function and passing in a parameter (also called jQuery, but this one is from the outside world and a reference to the actual jQuery itself). There is no pressing need to do this, but there are some advantages:




  • You can redefine a global parameter and give it a name that makes sense in the local scope.

  • There is a slight performance advantage since it is faster to look things up in the local scope instead of having to walk up the scope chain into the global scope.

  • There are benefits for compression (minification).



Earlier I described how these functions run automatically at startup, but if they run automatically who is passing in the arguments? This technique assumes all the parameters are defined as global variables. So if jQuery wasn't defined as a global variable this example would not work, and could not be called any other way since our example is an anonymous function. As you might guess, one things jquery.js does during it's initialization is define a 'jQuery' global variable, as well as it's more famous '$' global variable, which allows this code to work after jquery.js is included.



No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...