Javascript CoffeeScript 中的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6206820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Functions In CoffeeScript
提问by Nathan Campos
I'm trying to convert a function
from Javascript to CoffeeScript. This is the code:
我正在尝试将function
Javascript转换为 CoffeeScript。这是代码:
function convert(num1, num2, num3) {
return num1 + num2 * num3;
}
But how I can do that in CoffeeScript?
但是我如何在 CoffeeScript 中做到这一点?
I'm trying to run the function from an HTML source like this:
我正在尝试从这样的 HTML 源运行该函数:
<script type="text/javascript" src="../coffee/convert.js"></script>
<script type="text/javascript">
convert(6, 3, 10);
</script>
But it won't work and I get an error saying: ReferenceError: Can't find variable: convert
但它不起作用,我收到一条错误消息: ReferenceError: Can't find variable: convert
How to correct this?
如何纠正这个?
回答by lawnsea
You need to export the convert function to the global scope.
See How can Coffescript access functions from other assets?
您需要将转换函数导出到全局范围。
请参阅Coffescript 如何从其他资产访问函数?
window.convert = (num1, num2, num3) ->
num1 + num2 * num3
回答by jaime
@lawnsea answer is great.
@lawnsea 的回答很棒。
I just want to add some thoughts.
我只是想补充一些想法。
Instead of polluting the global namespace, I prefer to add just one variable to the window
object.
我宁愿只向window
对象添加一个变量,而不是污染全局命名空间。
window.App = {}
Then, you can have access to App
globally and add all your stuff there. the function convert
can now be expressed this way:
然后,您可以访问App
全局并在那里添加所有内容。该函数convert
现在可以这样表达:
App.convert = convert = (a, b, c) -> a + b * c
Then, to call the function within the local scope
然后,在本地范围内调用函数
convert 1,2,3
And now globally
而现在全球
App.convert 1,2,3
回答by twf
At the top level of your coffeescript file, this(aka @) should refer to window. So to attach it here, you could use the shorthand:
在您的 coffeescript 文件的顶层,this(又名@)应该指的是window。因此,要将其附加到此处,您可以使用简写:
@convert = (num1, num2, num3) -> num1 + num2 * num3
Note that this pollutes the global namespace, though. The solution posted by jm-is more prudent. But you can replace
请注意,这会污染全局命名空间。jm-发布的解决方案更加谨慎。但是你可以换
window.App = {}
with
和
@App = {}
The benefit of using @is that it refers to globalin node.js, so you can use the same code to expose your functions in both browser and serverside environments.
使用@的好处是它在 node.js 中引用global,因此您可以使用相同的代码在浏览器和服务器端环境中公开您的函数。
回答by edoloughlin
window.convert = (num1, num2, num3) ->
num1 + num2 * num3
回答by jrhicks
You should check these awesome slides just released today by godfoca http://www.slideshare.net/godfoca/lets-have-a-cup-of-coffeescriptAlso, you can try code out through-the-web at http://jashkenas.github.com/coffee-script/
您应该查看 godfoca 今天刚刚发布的这些很棒的幻灯片http://www.slideshare.net/godfoca/lets-have-a-cup-of-coffeescript此外,您可以在http://www.slideshare.net/godfoca/lets-have-a-cup-of-coffeescript尝试通过网络编写代码/jashkenas.github.com/coffee-script/
convert = (num1, num2, num3) ->
num1 + num2 * num3
回答by Jasmijn
convert = (num1, num2, num3) -> num1 + num2 * num3