LoginSignup
8

More than 5 years have passed since last update.

CoffeeScript で名前空間を定義

Posted at

他の JavaScript ライブラリとの名前衝突を回避するため、名前空間を定義できるようにしてみる。

まず名前空間 Fx にクラスを定義する関数を用意。

@Fx = (fn) ->
  klass = fn()
  @Fx[klass.name] = klass

クラス定義は次のように行う。

@Fx -> class Person
  constructor: (@firstName, @lastName) ->
    ...

使い方は予想通り。

person = new Fx.Person('John', 'Smith')

require.js とか使う方がいいのだろうか?

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8