Please activate JavaScript!
Please install Adobe Flash Player, click here for download

SAE Magazine

? mple. You’ll want to wrap called anonymous self- all know, functions keep selves as local vars. The a best practice to start lf-invoking-function.js us function, group it with p (function) right away. namespace thing as we that belongs to our plugin n’t class-based like Java, mous function as name- al); // -> i am global ere ; ); // -> undefined The code should be quite readable. Our function is able to gain access to anything defined in the global namespace, but anything it defines inside of its curly braces will remain private. Now we need to inject anything that these functions needs to work with: // File: 04-asif-complete.js (function($, window, undefined){ // code goes here })(jQuery, window); We pass in jQuery not $ to prevent any conflicts. Outside of our local namespace, one needs to be able to use $ for any other library without colliding with jQuerys’ noConflict mode. We use the window Object to be able to export properties out of the local namespace. Doing some- thing like this inside of the function would do this: window.myExport = ‘i am global’; Developers often call the window property some- thing like exports to make the code more readable. Note that the function takes three arguments, but we only pass in two of those when calling. We do not pass undefined, as any other script could have modified this. If undefined is not passed, and we try to receive it, what we’ll get is truly undefined. ➤ INDEX

Pages