从控制台调用coffeescript函数 [英] Calling coffeescript functions from console
问题描述
使用coffeescript和Rails 3.1.0.rc4玩一点。有此代码:
yourMom =(位置) - >
console.log location
yourMomwuz hur
当页面加载时,这将正确输出wuz hur。但是当我尝试调用
yourMom(wuz hur)
pre>
从chrome js控制台(我有时测试正常的JS函数),我得到一个ReferenceError:yourMom未定义
由coffeescript生成的函数是否可以这种方式提供?
解决方案一个更简单的方法来共享全局方法/变量是使用@这意味着这。
@yourMom =(位置) - >
console.log location
yourMomwuz hur
Nicer语法和更容易阅读,但我不鼓励你创建全局方法/变量
Playing a little with coffeescript and Rails 3.1.0.rc4. Have this code:
yourMom = (location) -> console.log location yourMom "wuz hur"
When the page loads, this outputs "wuz hur" properly. But when I try to call
yourMom("wuz hur")
from the chrome js console (as I do sometimes to test normal JS functions), I get a "ReferenceError: yourMom is not defined"
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
这篇关于从控制台调用coffeescript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!