Calling coffeescript functions from console(从控制台调用 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
这篇关于从控制台调用 coffeescript 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从控制台调用 coffeescript 函数


- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01