gpt4 book ai didi

javascript - Coffeescript 在函数中包装文件

转载 作者:数据小太阳 更新时间:2023-10-29 04:04:47 25 4
gpt4 key购买 nike

出于某种原因,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,我不会怀疑咖啡编译器,但它应该这样工作吗?如何

最佳答案

参见 my answer here关于在文件/模块之间共享 js 代码。另外仅供引用,包装函数是设计用来防止无意的全局变量。您可以通过将 --bare 传递给 coffee 编译器命令行工具来禁用 with,但这是有充分理由的最佳实践。

关于javascript - Coffeescript 在函数中包装文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7082373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com