最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Coffeescript wrapping files in a function - Stack Overflow

programmeradmin4浏览0评论

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

class TestClass
    constructor: (@value) ->

    printValue: () ->
        alert(@value)

printAValue = () -> 
    test = new TestClass()
    test.printValue()

then I get test.js:

(function() {
  var TestClass, printAValue;
  TestClass = (function() {
    function TestClass(value) {
      this.value = value;
    }
    TestClass.prototype.printValue = function() {
      return alert(this.value);
    };
    return TestClass;
  })();
  printAValue = function() {
    var test;
    test = new TestClass();
    return test.printValue();
  };
}).call(this);

My simple html file won't work with this:

<html>
    <head>
        <script src="test.js"></script>
    </head>
    <body onload="printAValue()">
    </body>
</html>

I haven't worked with much JS before, and I wouldn't doubt the coffee piler, but is the way it should work? How

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

class TestClass
    constructor: (@value) ->

    printValue: () ->
        alert(@value)

printAValue = () -> 
    test = new TestClass()
    test.printValue()

then I get test.js:

(function() {
  var TestClass, printAValue;
  TestClass = (function() {
    function TestClass(value) {
      this.value = value;
    }
    TestClass.prototype.printValue = function() {
      return alert(this.value);
    };
    return TestClass;
  })();
  printAValue = function() {
    var test;
    test = new TestClass();
    return test.printValue();
  };
}).call(this);

My simple html file won't work with this:

<html>
    <head>
        <script src="test.js"></script>
    </head>
    <body onload="printAValue()">
    </body>
</html>

I haven't worked with much JS before, and I wouldn't doubt the coffee piler, but is the way it should work? How

Share Improve this question asked Aug 16, 2011 at 17:29 DrPepperDrPepper 2372 silver badges6 bronze badges 3
  • Try changing your coffeescript line to pass a value to the TestClass initializer -test = new TestClass('hello world') – arunkumar Commented Aug 16, 2011 at 17:34
  • See [my answer here][1] on sharing code between JS files/modules. [1]: stackoverflow./questions/6951438/… – Peter Lyons Commented Aug 16, 2011 at 18:15
  • This is by far the most popular question asked about CoffeeScript on SO. See stackoverflow./q/6481986/66226, stackoverflow./q/4214731/66226, stackoverflow./q/5693211/66226... – Trevor Burnham Commented Aug 16, 2011 at 18:18
Add a ment  | 

2 Answers 2

Reset to default 9

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 piler mand line tool, but it is a best practice with good reason.

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)

  1. 暂无评论