Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function thatYouWantToExport() {
};
if (typeof exports === 'undefined') {
window.myFn = thatYouWantToExport;
} else {
exports.myFn = thatYouWantToExport;
}
@heapwolf

This comment has been minimized.

Copy link

heapwolf commented May 10, 2011

;(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.

@indutny

This comment has been minimized.

Copy link
Owner Author

indutny commented May 10, 2011

I don't like usage of this. Btw, you missed one right-closing parenthesis and have one excessive

@heapwolf

This comment has been minimized.

Copy link

heapwolf commented May 10, 2011

;(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);

@indutny

This comment has been minimized.

Copy link
Owner Author

indutny commented May 10, 2011

Oh, sorry. You're right.

Missed that you'd started with ;(

@heapwolf

This comment has been minimized.

Copy link

heapwolf commented May 10, 2011

;(function(env) { return env.myLib = function myLib() { /.../ }; }(window || module.exports));

seems like a pretty nice solution if you dont like this.

@indutny

This comment has been minimized.

Copy link
Owner Author

indutny commented May 10, 2011

This will throw ReferenceError: window is not defined

@heapwolf

This comment has been minimized.

Copy link

heapwolf commented May 10, 2011

oh yeah, i think thats why i opted for |this| instead.

@heapwolf

This comment has been minimized.

Copy link

heapwolf commented May 10, 2011

if you come up with a clever way to not use | this | let me know ;)

@indutny

This comment has been minimized.

Copy link
Owner Author

indutny commented May 10, 2011

this.exports || this === (typeof exports === 'undefined' ? window : exports)
@heapwolf

This comment has been minimized.

Copy link

heapwolf commented May 10, 2011

hehe, my original code is looking pretty good at this point ;)

@indutny

This comment has been minimized.

Copy link
Owner Author

indutny commented May 10, 2011

Your truth :) Just like my code more than yours :D

@heapwolf

This comment has been minimized.

Copy link

heapwolf commented May 10, 2011

hehe, thats how it is, everyone prefers their own code ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.