Created
July 5, 2012 19:05
-
-
Save anderssvendal/3055732 to your computer and use it in GitHub Desktop.
Easy namespacing of classes and objects.
This file contains 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
# Easy namespacing of classes and objects. | |
# | |
# name - The String containing namespaces up to but not including the class | |
# name. | |
# | |
# Examples | |
# # Using a class | |
# class module("Foo.Bar").Baz | |
# doStuff: -> | |
# console.log "doing stuff" | |
# | |
# baz = new window.Foo.Bar.Baz() | |
# baz.doStuff() | |
# | |
# # Using an object | |
# module("Foo.Bar").Baz = | |
# doStuff: -> | |
# console.log "doing stuff" | |
# | |
# window.Foo.Bar.Baz.doStuff() | |
window.module = (name) -> | |
segments = name.split(/\./) | |
# Make sure all the needed objects are created before assigning the class | |
root = window | |
for segment in segments | |
root[segment] ?= {} | |
root = root[segment] | |
root |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Coolio. Thanks!