Permalink
Browse files

Add module-esque namespacing support.

  • Loading branch information...
0 parents commit f21ffbd76645941b1ea8a9c66417dad04fab821b @tav committed Oct 14, 2012
Showing with 15 additions and 0 deletions.
  1. +15 −0 define.coffee
15 define.coffee
@@ -0,0 +1,15 @@
+# Public Domain (-) 2011 The Jsutil Authors.
+# See the Jsutil UNLICENSE file for details.
+
+# The `define` function provides a utility wrapper to namespace code blocks. If
+# an explicit target isn't passed in, it defaults to using `window` or `exports`
+# as the top-level object.
+#
+# This functionality has been adapted from the `namespace` function defined in
+# the [CoffeeScript FAQ](https://github.com/jashkenas/coffee-script/wiki/FAQ).
+define = (root, name, constructor) ->
+ [root, name, constructor] = [(if typeof exports isnt 'undefined' then exports else window), arguments...] if arguments.length < 3
+ target = root
+ target = target[item] or= {} for item in name.split '.'
+ constructor target, root
+ return

0 comments on commit f21ffbd

Please sign in to comment.