【发布时间】:2011-10-28 06:48:30
【问题描述】:
出于某种原因,coffeescript 编译器在编译时将我所有的 .coffee 文件包装在一个函数中。例如,如果我有 test.coffee:
class TestClass
constructor: (@value) ->
printValue: () ->
alert(@value)
printAValue = () ->
test = new TestClass()
test.printValue()
然后我得到 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);
我的简单 html 文件不适用于此:
<html>
<head>
<script src="test.js"></script>
</head>
<body onload="printAValue()">
</body>
</html>
我以前没有使用过很多 JS,我不会怀疑咖啡编译器,但它应该是这样工作的吗?如何
【问题讨论】:
-
尝试更改您的咖啡脚本行以将值传递给 TestClass 初始化程序 -
test = new TestClass('hello world') -
在 JS 文件/模块之间共享代码见 [我的回答][1]。 [1]:stackoverflow.com/questions/6951438/…
-
这是迄今为止关于 SO 上的 CoffeeScript 的最受欢迎的问题。见stackoverflow.com/q/6481986/66226、stackoverflow.com/q/4214731/66226、stackoverflow.com/q/5693211/66226...