Created
March 2, 2014 21:28
-
-
Save romac/9314244 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://stackoverflow.com/a/9912727 | |
# Code: | |
# | |
namespace = (target, name, block) -> | |
[target, name, block] = [(if typeof exports isnt 'undefined' then exports else window), arguments...] if arguments.length < 3 | |
top = target | |
target = target[item] or= {} for item in name.split '.' | |
block target, top | |
# Usage: | |
# | |
namespace 'Hello.World', (exports) -> | |
# `exports` is where you attach namespace members | |
exports.hi = -> console.log 'Hi World!' | |
namespace 'Say.Hello', (exports, top) -> | |
# `top` is a reference to the main namespace | |
exports.fn = -> top.Hello.World.hi() | |
Say.Hello.fn() # prints 'Hi World!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment