-
-
Save indutny/964299 to your computer and use it in GitHub Desktop.
function thatYouWantToExport() { | |
}; | |
if (typeof exports === 'undefined') { | |
window.myFn = thatYouWantToExport; | |
} else { | |
exports.myFn = thatYouWantToExport; | |
} |
I don't like usage of this
. Btw, you missed one right-closing parenthesis and have one excessive
;(function(env) { return env.myLib = function myLib() { /.../ }; } (this.exports || this) );
actually this is the correct number of parenthesis, this is called a IIFE (Immediately Invoked Function Expression) alternatively you can write
;(function(env) { return env.myLib = function myLib() { /.../ }; })(this.exports || this);
but it will not validate. alternatively you could do this,
;(function(env) { return env.myLib = function myLib() { /.../ }; })(window || module.exports);
Oh, sorry. You're right.
Missed that you'd started with ;(
;(function(env) { return env.myLib = function myLib() { /.../ }; }(window || module.exports));
seems like a pretty nice solution if you dont like this.
This will throw ReferenceError: window is not defined
oh yeah, i think thats why i opted for |this| instead.
if you come up with a clever way to not use | this | let me know ;)
this.exports || this === (typeof exports === 'undefined' ? window : exports)
hehe, my original code is looking pretty good at this point ;)
Your truth :) Just like my code more than yours :D
hehe, thats how it is, everyone prefers their own code ;)
;(function(env) { return env.myLib = function myLib() { /.../ }; }(this.exports || this));
this provides a closure for encapsulation/variable safety, environment detection and passes the execution context pointer in as a parameter that can be obfuscated.