简体   繁体   English

在CoffeeScript / JavaScript中导入名称空间

[英]Import namespace in CoffeeScript / JavaScript

Is it possible to import a namespace for a JavaScript / CoffeeScript file? 是否可以为JavaScript / CoffeeScript文件导入名称空间?
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: 这是CoffeeScript文件,我希望避免在其中重复输入完全限定的名称空间:

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 我可以导入类似于您在Java中导入包的方式的名称空间吗

import Editor.*;

Try something like this: 尝试这样的事情:

Editor.coffee: Editor.coffee:

Editor = {}

root = exports ? window
root.Editor = Editor

main.coffee: main.coffee:

{Editor} = require './Editor'

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. 因为您可以重新定义您真正需要的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM