8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CoffeeScriptでTypeScriptのmoduleっぽい名前空間の初期化

Posted at

名前空間の初期化をファイル読み込み順番を気にせずにやりたい
ただし継承関係は気にする必要があるので、気をつけることには変わりない。

実装

root = window ? global ? this
root._module_ = (ns, f) =>
  context = root
  hist = []
  for name in ns.split('.')
    unless context[name]?
      context[name] = {}
    context = context[name]
    hist.push context
  f.apply context, hist

使い方


# base.coffee
_module_ "App.View", ->
  class @Base

# myview.coffee
_module_ "App.View", (App, View) ->
  class @MyView extends View.Base

# main.coffee
myview = new App.View.MyView
8
8
0

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
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?