Home

Awesome

Namespace-in-CoffeeScript

Elegant pattern to simulate namespace in Coffeescript

How to use in the browser :

namespace Foo:
  class Bar extends Baz
    #[...]
namespace Foo:SubPackage1:SubPackage2:
  class Bar extends Baz
    #[...]

or

module Foo:
  class Bar extends Baz
    #[...]
module Foo:SubPackage1:SubPackage2:
  class Bar extends Baz
    #[...]

then

instance = new Foo.Bar
other    = new Foo.SubPackage1.SubPackage2.Bar

Notice : Don't use the same name for your namespace and your class to avoid collisions.

How to use in CommonJS environment :

require './path/to/this/file' # require once in your application

Then use you can use it as on the browser. But you can't use the "module" notation because it's already reserved by the CommonJS environnement. Use "namespace" instead.

You can specify that you want to use the global namespace. So your code will be exposed to the correct root : window or global for CommonJS environment. So you don't need to write something like root = global ? window, root.myClass = myClass under your class.

namespace global:
  class myClass