CoffeeScript 中的函数

2022-01-24 00:00:00 javascript coffeescript

我正在尝试将 function 从 Javascript 转换为 CoffeeScript.这是代码:

I'm trying to convert a function from Javascript to CoffeeScript. This is the code:

function convert(num1, num2, num3) {
    return num1 + num2 * num3;
}

但是我如何在 CoffeeScript 中做到这一点?

But how I can do that in CoffeeScript?

我正在尝试从这样的 HTML 源运行该函数:

I'm trying to run the function from an HTML source like this:

<script type="text/javascript" src="../coffee/convert.js"></script>

<script type="text/javascript">
    convert(6, 3, 10);
</script>

但它不起作用,我收到一条错误消息:ReferenceError: Can't find variable: convert

But it won't work and I get an error saying: ReferenceError: Can't find variable: convert

如何解决这个问题?

推荐答案

需要将convert函数导出到全局作用域.
请参阅 Coffescript 如何从其他资产访问函数?

You need to export the convert function to the global scope.
See How can Coffescript access functions from other assets?

window.convert = (num1, num2, num3) ->
  num1 + num2 * num3

相关文章