I know I can create a self-invoking, nested function like this
(function ns(){
(function Class(){
alert('ns.Class fired');
})();
})();
But it’s ugly, and doesn’t look quite right.
My js chops aren’t what they should be, and I’m hoping someone can show me a better way to structure my namespaced app and still be able to utilize “some” self invoking functions.
// THIS DOESN'T WORK
// because I haven't called "ns"
function ns(){
(function Class(){
alert('fired');
})();
};
For my purposes, the reason I’m asking this is to better namespace my JS in conjunction with jQuery.
So I’d like to be able to do something like this (which DOES WORK)
var ns = ns || {};
ns.Class = function(){};
ns.Class.Navigation = (function() {
$('#element').on('click', function() {alert('element clicked');});
})();
But I’m not sure if this is the right way to structure larger (read: js heavy) apps?!?!
- Is the weight of this too heavy?
- Is there a smarter way to achieve this?
Well, First off: the fact that your calling the “main” IIFE
nssuggests that you think of it as a namespace object, which isn’t entirely correct. Namespaces are often created using IIFE’s because they have the added benefit of closure scope(s). But in the end, namespaces are just a fancy word for Object (literals).Take the most popular lib/toolkit/framework out there: jQuery. Basically, it’s one huge IIFE, that constructs an equally vast object, that is assigned a number of methods and properties (well, references to function objects anyway). There are some other objects and variables created in that IIFE, but they are either not exposed to the global object at all, or (Very) indirectly.
Allow me to clarify:
This uses some of the basic principals all toolkits/libs, and actually all decent JS scripts share: using functions as first class object, using them as expressions to create a temporary scope etc…)
IMO, it makes a good case for IIFE’s: the scope gives you plenty of time to assign any object to a variable, so regardless of How you create a method (with or without an IIFE), you don’t have to worry about what
thisreferences at any given time, just use the variables.You can implement some basic data hiding, too. In this example the initial value of
semiExposedis being passed to an IIFE, and preserved within its scope. Nothing can muck this up (well, that’s not quite true at the moment), so you can allways revert to the initial values of any property.However, I will admit, IIFE’s can make your code harder to read as it grows, and I completely understand why you’d not want to use them too much. You could look into
bind, it’ll help you cut back on many IIFE’s, but there is a down-side. Some ppl still use IE8, for example, which doesn’t supportbind.But an other option would be: create a simple IIFE factory function:
That way, you can get rid of some IIFE’s, by replacing them with a simple function call to which you pass an object. Easy, and X-browser compatible.
Lastly, your first snippet is something that I do tend to use in event delegation:
That way, I don’t have to create another veriable in the same scope, my targetRef is assigned to
parentDivwhen the div is found, and targetRef is GC’ed, I’m finished with it, so there’s no need for that variable to stay in scope.It’s getting rather late now, and I don’t know if I’m making much sense at all. Bottom line is: You might hate IIFE’s, but you can’t really do without them.
If it’s the mass of parentheses that bother you, you might be glad to know that you don’t have to use them. Any operator that forces the JS engine to interpret the function declaration as an expression will do:
Perhaps you prefer an alternative notations? But honestly: you may not like the syntax, but I’m afraid you’re going to have to live with it, or switch to coffeescript or something.