Benefits of Anonymous JavaScript Namespaces

Is there any use in writing JavaScript classes and namespaces of this ...

if(typeof MyNamespace === 'undefined'){ var MyNamespace = {}; } (function(){ MyNamespace.MyClass = function(){ this.property = 'foo' return this; } }()); 

In comparison with this ...

 if(typeof MyNamespace === 'undefined'){ var MyNamespace = {}; } MyNamespace.MyClass = function(){ this.property = 'foo' return this; } 

I saw the first template implemented in several libraries, and tried to find how to get additional added benefit if some other function was declared inside an anonymous function in the first example.

+6
source share
4 answers

To your question:

Yes, there is a difference (and benefit). In the first example, you can control access control (which means using prototypes based on variables and public / private member functions). As an example:

 var m = (function() { var o = {}; o.myPublicProperty = 0; // can be accessed by instantiated object calling code var myPrivateProperty = 1; // can't be accessed outside of this module o.myPublicFunction = function() { myPrivateFunction(); return myPrivateProperty; }; function myPrivateFunction() { ++myPrivateProperty; ++o.myPublicProperty; } o.getMyPrivateProperty = function() { return myPrivateProperty; } return o; })(); console.log(m.myPublicProperty); // 0 console.log(m.getMyPrivateProperty()); // 1 console.log(m.myPrivateProperty); // undefined console.log(m.myPublicFunction()); // increments console.log(m.myPublicProperty); // 1 console.log(m.getMyPrivateProperty()); // 2 

http://jsfiddle.net/dbrecht/EQ4Tb/

A little off topic, but this is a bit strange for me:

 if(typeof MyNamespace === 'undefined'){ var MyNamespace = {}; } 

Why not just use: var MyNamespace = MyNamespace || {}; var MyNamespace = MyNamespace || {}; ?

+11
source

I am not entirely sure of the other advantages, but everything declared in an anonymous function will remain in this area, that is, not declared in the global area. It can be beneficial.

0
source

In the simple case, you showed that the anonymous function being executed immediately does not give any advantages.

However, you can declare variables or other functions within the scope of anonymous functions, and they will be effectively closed to your MyClass function. So there is a huge advantage, and even if you don’t need private variables now, you can later so that you can use the anonymous function anyway ...

Note also that placing a var statement inside an if is pointless, since the declaration (but not the destination) gets "raised" from the block.

0
source

Yes, private variables.

 var MyNamespace = MyNamespace || {}; (function(){ var priv_var = 'bar'; MyNamespace.MyClass = function(){ this.property = 'foo'; //priv_var is accessible in here return this; } }()); 

Versus:

 var MyNamespace = MyNamespace || {}; var priv_var = 'bar'; MyNamespace.MyClass = function(){ this.property = 'foo'; //priv_var is accessible anywhere return this; } 
0
source

Source: https://habr.com/ru/post/901752/


All Articles