2

I am looking into systems for making JavaScript libraries. I have seen most of the libraries using methods such as "Immediately Invoked Function Expression". This method to me makes the code quite unreadable.

I want to know what are the advantages of using this method?

And what are limitations of using the basic .prototype system for creating a library?

For instance, what is wrong with using this pattern to create a library?:

function Library(){

  this.property = 'val';

}

Library.prototype.method = function(){
  // a method
}

//and to use the library
var lib = new Library();
2
  • Advantages compared to what? To prototype-less constructors? Also, do you actually want to create a singleton module, or a multiple-times instantiable constructor? Commented Jul 30, 2014 at 11:20
  • IIFE creates a closure scope so many variables that are global to your module are not global (part of window). Note that in the answer the private variable is shared and all instances will use the same foo. More on prototype and constructor functions here: stackoverflow.com/questions/16063394/… Commented Jul 30, 2014 at 12:41

1 Answer 1

2

Immediately-executed functions are essentially just a means of avoiding polluting the global scope with variables that should be private.

Your approach is fine, but what if you want your library to internally reference private data that should not be part of your public API? Example:

var Some_class = (function() {

    var foobar = 'foo'; //private; library can read it but public API can't

    var api = function() { /* constructor */ };
    api.prototype.method = function() { return foobar == 'foo' ? 1 : 0; }

    return api;

})();

var instance = new Some_class();
instance.method();

foobar is a private (i.e. internal) variable for use BY your library, but not publicly accessible via its API.

Sign up to request clarification or add additional context in comments.

6 Comments

so with this approach. instance.foobar will not be accessible? So, the advantage of using IIFE(immediately invoked function expression) is to prevent exposure of internal properties of a library and therefore making it less fragile? @Utkanos
Yes to both questions. In a proper OO environment e.g. PHP you would have visibility flags on vars; JS doesn't have a true implementation of that, so this is how you would simulate private properties.
This brings out another question, is there any performance difference between the two approaches? @Utkanos
Well that depends on many other factors; the quality of your overall code; its efficiency; the footprint of it; nominally, no, there would be negligible difference in principle.
@Utkanos what would be the best way to create a js library in ES6/ES7
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.