Private Functions In JavaScript
Written by Ian Elliot   
Friday, 03 February 2012
Article Index
Private Functions In JavaScript
Closure

Using a closure to create a private function

There is another way to create a private function that avoids the need to recreate the inner functions each time it is called, however it is more complicated and it is not always worth the effort.

You can use the well known method to create private variables in objects and functions via closure.

To do this we need to create a function factory that returns the function we actually want to use, i.e. myMain in the previous examples. The reason we do this is because closure ensures that the returned function has access to all of the variables that were in scope when it was created - and this includes other functions.

For example:

function myMainFactory(){
function myHelper(count){
alert(count);
count++;
}
return function(){
var count=0;
count++;               
myHelper(count);
alert(count);
}
}

You can see that myMainFactory defines a function called myHelper an returns a function which is essentially the old myMain. As this creates a closure the returned function has access to myHelper and so it is legal for it to call it.

To use the function factory you would write:

var myMain=myMainFactory();
myMain();

Notice that now each time you call myMain the myHelper isn't recreated because it exists within the execution context for the myMain function for all the time that the myMain variable exists.

Also notice that as myHelper is no longer an inner function of myMain it doesn't have access to its local variables. In other words the relationship between the functions is no longer that of inner and outer functions but of function and closure.

This is easy enough to follow but it isn't good to have a factory function with a different name to the function returned. A more sophisticated solution is to use the trick of immediate function evaluation. This works because we don't actually want an explicit function called myMainFactory because we only ever want to create a single instance of myMain:

var myMain=(function(){
function myHelper(count){
alert(count);
count++;
}
return function(){
var count=0;
count++;               
myHelper(count);
alert(count);
}
})();

Notice that what we have here is essentially the function factory but now it is an anonymous function that is called as soon as it is defined - apart from this change it all works in the same way.

Public helpers

Although the main focus of this article is on making functions private there is another way that helper functions can avoid polluting the global namespace but in this case they are still publicly accessible.

Obviously, you can mix these techniques depending on whether the function needs to be public or private.

This technique depends on the simple fact that functions are first class objects. This means you can define properties and methods within a function as for any object. So, for example, you could define myHelper as a method of the myMain function object:

myMain.myHelper=function(count){
alert(count);
count++;
}    
myMain();

function myMain(){
var count=0;
count++;               
myMain.myHelper(count);
alert(count);
}

Notice that you can define the myHelper method on the myMain object because the function definition is moved to the top. Even so it looks strange and a better way to write the code is:

function myMain(){
var count=0;
count++;               
myMain.myHelper(count);
alert(count);
}
myMain.myHelper=function(count){
alert(count);
 count++;
}    
myMain();

 

Of course as myHelper isn't an inner function it doesn't have access to myMain's local variables. Similarly myMain doesn't have access to myHelper's local variables. The rules are exactly the same as for any object.

Finally, with the mention of objects, it is often worth implementing what looks like a function library as a set of objects and the techniques described above shouldn't be used just to avoid using a good object-oriented design.

 

Banner

Related Articles

JavaScript Doesn't Need Class

JavaScript Books (2012)

 

espbook

 

Comments




or email your comment to: comments@i-programmer.info

 

To be informed about new articles on I Programmer, subscribe to the RSS feed, follow us on Google+, Twitter, Linkedin or Facebook or sign up for our weekly newsletter.


<ASIN:0980285844>

<ASIN:0596805527>

<ASIN:0321700953>

<ASIN:0596517742>

<ASIN:0470647833>


 

 

 

 

 

 

 



Last Updated ( Saturday, 04 February 2012 )