从控制台调用 coffeescript 函数

使用 coffeescript 和 Rails 3.1.0.rc4.有这个代码:

Playing a little with coffeescript and Rails 3.1.0.rc4. Have this code:

yourMom = (location) ->
  console.log location

yourMom "wuz hur"

当页面加载时,这会正确输出wuz hur".但是当我尝试打电话时

When the page loads, this outputs "wuz hur" properly. But when I try to call

yourMom("wuz hur")

从 chrome js 控制台(就像我有时测试正常的 JS 函数一样),我收到ReferenceError: yourMom is not defined"

from the chrome js console (as I do sometimes to test normal JS functions), I get a "ReferenceError: yourMom is not defined"

coffeescript 生成的函数可以这样使用吗?

Are functions generated by coffeescript available in this way?

推荐答案

共享全局方法/变量的更简单方法是使用@,这意味着.

an easier way to share global methods/variables is to use @ which means this.

@yourMom = (location) ->
  console.log location

yourMom "wuz hur"

更好的语法和更容易阅读,但我不鼓励你创建全局方法/变量

Nicer syntax and easier to read, but I don't encourage you to create global methods/variables

相关文章