Cross - platform wrapper function for JavaScript libraries
Thursday, October 25, 2012
0
comments
In old times, when JavaScript libraries had to work only in the web - browsers, common pattern for exporting your stuff into the global namespace was looking like this:
We were just attaching our methods and properties to the global namespace, and this was all fine before somebody come up with the idea to run JavaScript code in non - browser environment. There was no window object so we had to find global object ourselves:
Then things started to change very quickly, CommonJS standard and it's implementations against AMD (Asynchronous Module Definition), RequireJS and so on, but how should I package my library so the end - users won't have to repackage it in order to use it?
(function() { window['MyLibraryMethod'] = function() { alert('HELLO WORLD!'); }; })();
We were just attaching our methods and properties to the global namespace, and this was all fine before somebody come up with the idea to run JavaScript code in non - browser environment. There was no window object so we had to find global object ourselves:
(function(global) { global['MyLibraryMethod'] = function() { alert('HELLO WORLD!'); }; })(function() { return this; }.call(null));
Then things started to change very quickly, CommonJS standard and it's implementations against AMD (Asynchronous Module Definition), RequireJS and so on, but how should I package my library so the end - users won't have to repackage it in order to use it?
Labels:
CommonJS,
dependencies,
ecmascript-5,
hacks,
Histone,
Java,
JavaScript,
Mozilla Rhino,
RequireJS

