Skip to content

Instantly share code, notes, and snippets.

@DyegoCosta
Created July 13, 2012 02:43
Show Gist options
  • Save DyegoCosta/3102364 to your computer and use it in GitHub Desktop.
Save DyegoCosta/3102364 to your computer and use it in GitHub Desktop.
CoffeeScript namespacing
namespace = (target, name, block) ->
[target, name, block] = [exports ? window, arguments...] if arguments.length < 3
top = target
target = target[item] or= {} for item in name.split '.'
block target, top
class Foo
constructor: (@message) ->
Bar: ->
alert @message
namespace "Coffee", (exports) ->
exports.Foo = Foo
$('#start').click ->
foo = new Coffee.Foo "Test!"
foo.Bar()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment