Skip to content

Instantly share code, notes, and snippets.

@bahelms
Last active August 29, 2015 14:00
Show Gist options
  • Save bahelms/0989756c71de02e70886 to your computer and use it in GitHub Desktop.
Save bahelms/0989756c71de02e70886 to your computer and use it in GitHub Desktop.
How to namespace in CoffeeScript
# Node.js is exports; Rails is window
global = if typeof exports isnt 'undefined' then exports else window
global.namespace = (target, name, block) ->
[target, name, block] = [global, arguments...] if arguments.length < 3
top = target
target = target[item] or= {} for item in name.split '.'
block target, top
# Declare your class
namespace "NS", (global) ->
class global.HtmlCanvas
# Use your class
NS.HtmlCanvas()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment