2

Is it possible to import a namespace for a JavaScript / CoffeeScript file?
The idea is to avoid typing out the fully qualified namespaces.

Let's say I have defined the bellow in my init code.

window.Editor = { }

And this is the CoffeeScript file in which I want to avoid having to type the fully qualified namespaces over and over again:

class Editor.Editor
  constructor: (@width, @hight) ->
    @canvas = new Editor.Canvas(@width,  @hight)
    @backGround = new Editor.BackGround(@canvas)
    @frontGround = new Editor.FrontGround(@canvas)

Can I import a namespace similar to how you would import a package in Java for example

import Editor.*;
4

2 に答える 2

1

次のようなことを試してください:

Editor.coffee:

Editor = {}

root = exports ? window
root.Editor = Editor

main.coffee:

{Editor} = require './Editor'
于 2012-12-09T15:20:34.507 に答える
0

あなたが試すことができます

for key, value of Editor
  window[key] = value

しかし、あなたは本当に注意する必要があります。本当に必要なものを再定義できるからです。

于 2012-12-09T14:40:23.427 に答える