The suggestion on the jQuery documentation page doesn’t wrap jQuery,
it wraps your code and passes the jQuery object to it. It is based on
the idea that there is a major conflict between a number of libraries
as a result of their use of “$” as the name of a function that is
fundamental to their operation. jQuery creates a global $ varaible
that is assigned a reference to the jQuery object so programmers can
write $() instead of jQuery().
One strategy suggested is to use:
(function($){ /* code using $ */})(jQuery);
However, it doesn’t fix the conflict, it just makes it manageable. A
consequence is that it makes it more difficult (though not impossible)
to use the $ function of other libraries from inside the anonymous
function. Given the importance of $ to those libraries (and its
frequent use by them internally as a global variable) it might not be
a good strategy.
A more robust version is:
var global = (function(){return this})();
but it may not matter very often.
You are only “isolated” to the extent that variables that would have
been declared as global are now local to the anonymous function. The
conflicts are still there, just that the local variables mask them.
Undeclared variables (bad practice but it happens) get no isolation
and may behave quite differently to what you expect (or not, depending
on your knowledge of ECMAScript).
You can still create global variables (global.varName = ...), but the
idea is to keep it all within the same scope.
It’s a double-edged sword though - you may *want* to use the masked
variables (you can get at them using global.varName or similar) but
because the code is executed immediately, they may not be available
yet. If you have conflicts with names, you may not be ablet to
control the order scripts are loaded in either.
It makes conflicts manageable and can be used to create public and
private variables.
Put your library and all the code that you are going to execute inside
an anonymous function:
(function() {
// declare myLib
var myLib = {...};
// now use it.
myLib.go(...);
// and so on
})();
There are other approaches, e.g. put your code in the same file as the
library and ensure it loads first. It would be good if HTML script
elements allowed:
<script src=”myLib.js”> code that uses myLib</script>
But it doesn’t.
You need to dynamically add any listeners that will call functions in
your library and keep references using closures - trying to call
myLib.go() from an inline listener will error. Adding listeners
dynamically leaves you open to memory leaks in IE if not done
properly.
It might be worth considering if you have others adding code that you
have no control over to pages that your code is to be run in. But
naming conflicts are only one issue there, you need to work out how
you’ll work together to avoid other issues (e.g. overwriting
listeners) and can therefore easily determine a strategy for avoiding
name conflicts anyway. Otherwise, don’t bother. Patterns for public
and private variables should be used on their own merits, not in the
belief that they are a magic bullet that solves naming conflicts.
See above. Also, it’s useful for creating public and private
variables using functions with closures and to execute code that, once
run, doesn’t need to do anything further. It might also help to ask
about the disadvantages.
Depending on how you implement it, it becomes more complex to write
and maintain, e.g. in-line handlers may not be able to call your
functions so they have to be added dynamically, probably using
window.onload or similar, and keep references using closures. It may
use a lot more memory but not necessarily.
You may still get conflicts with global variables from another
library’s functions because your library’s functions may mask them.
The maintenance issue shouldn’t be underestimated - any time you
introduce more complex code patterns, you run the risk that some
future coder won’t understand it and stuff the whole thing up. It’s
usually OK in a library function that *should* be maintained by
someone who knows what they are doing, but to use it as a general
pattern in a page that will change frequently by persons unknown may
be more trouble than it’s worth. It’s rare for a good programmer to
last on maintenance tasks for very long - either they are put to more
profitable uses when their talent is noticed or they lose interest and
go elsewhere.
There’s a lot to be said for the ancient “keep it simple,
stupid” (KISS) principle. As always, you have to weigh up the pros
and cons.
Clear!