咖啡脚本编译器在编译所有的 .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,虽然我不怀疑咖啡编译器,但这样的工作方式是否正确呢?如何做到呢?
test = new TestClass('hello world')
。 - arunkumar