Coffeescript wrapping files in a function

后端 未结 2 1740
清酒与你
清酒与你 2020-12-30 11:23

The coffeescript compiler is, for some reason, wrapping all of my .coffee files in a function when they are compiled. For example, if I have test.coffee:

cla         


        
相关标签:
2条回答
  • 2020-12-30 11:49

    See my answer here on sharing jS code between files/modules. Also FYI the wrapper function is by design to prevent unintentional global variables. You can disable with by passing --bare to the coffee compiler command line tool, but it is a best practice with good reason.

    0 讨论(0)
  • 2020-12-30 11:51

    Never add event listeners in HTML. Add them in your JavaScript, preferably in the same scope in which you define the event handler.

    printAValue = () -> 
        test = new TestClass()
        test.printValue()
    
    document.body.addEventListener('load', printAValue, false)
    

    If you absolutely need to export something to the global scope, export to the window object:

    window.printAValue = () -> 
        test = new TestClass()
        test.printValue()
    
    0 讨论(0)
提交回复
热议问题