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.*;

2 Answers 2

1

Try something like this:

Editor.coffee:

Editor = {}

root = exports ? window
root.Editor = Editor

main.coffee:

{Editor} = require './Editor'
Sign up to request clarification or add additional context in comments.

Comments

0

You can try

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

But you should be really carefull. Because you can redefine something you really need.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.