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
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 动态更新多个选择框 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 我什么时候应该在导入时使用方括号 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01