从控制台调用 coffeescript 函数 [英] Calling coffeescript functions from console

查看:19
本文介绍了从控制台调用 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 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆