【问题标题】:Calling functions from `coffee` executable从 `coffee` 可执行文件调用函数
【发布时间】:2017-10-27 18:24:20
【问题描述】:

请原谅这个菜鸟问题,但为什么我不能(几乎)从coffee REPL(或者从在 TextMate 中编写和运行的文件)调用任何标准函数?

变量赋值有效,函数无效。

例子:

coffee> string = "string"
'string'
coffee> list = [1,2,3]
[ 1, 2, 3 ]
coffee> num = 42
42
coffee> opposite = true
true
coffee> num = -42 if opposite
-42

但是

coffee> alert "Hello, World"
ReferenceError: alert is not defined
    at repl:1:5
    at REPLServer.replDefaults.eval (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/repl.js:33:28)
    at repl.js:239:12
    at Interface.<anonymous> (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/repl.js:62:9)
    at Interface.EventEmitter.emit (events.js:117:20)
    at Interface._onLine (readline.js:202:10)
    at Interface._line (readline.js:531:8)
    at Interface._ttyWrite (readline.js:760:14)
    at ReadStream.onkeypress (readline.js:99:10)
    at ReadStream.EventEmitter.emit (events.js:117:20)

coffee> print "Hello"
ReferenceError: print is not defined
    at repl:1:5
    at REPLServer.replDefaults.eval (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/repl.js:33:28)
    at repl.js:239:12
    at Interface.<anonymous> (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/repl.js:62:9)
    at Interface.EventEmitter.emit (events.js:117:20)
    at Interface._onLine (readline.js:202:10)
    at Interface._line (readline.js:531:8)
    at Interface._ttyWrite (readline.js:760:14)
    at ReadStream.onkeypress (readline.js:99:10)
    at ReadStream.EventEmitter.emit (events.js:117:20)

真正让我感动的是:

coffee> console.log "Help!"
Help!
undefined

我通过 Homebrew 安装了 Node,并通过 npm (全局)安装了 CoffeeScript。

【问题讨论】:

  • 使用node时,console.log是获取输出的正常方式。如果您喜欢使用该名称,您可以定义print=console.logconsole.log 会打印或显示您想要的内容。但它也返回undefined。但在脚本中使用时您不会看到。

标签: node.js coffeescript textmate read-eval-print-loop


【解决方案1】:

alert 不是 javascript 的功能。它是浏览器 向 JavaScript 公开的 API 的一部分。而您计算机上命令行中的coffee 只是node.js 的一个薄包装器,它将咖啡脚本翻译成javascript 以由节点解释。 node.js 不提供 alert 函数。也不提供全局的print 函数。

节点和浏览器都在全局范围内提供console 对象。所以console.log 的工作原理是一样的。

查看node docs 了解哪些功能节点公开。请记住,仅仅因为它可以在浏览器中运行并不意味着它可以在节点中运行。

【讨论】:

  • 那为什么我阅读的每个 CoffeeScript 教程都使用 alert 就好像你可以直接调用它一样?我不要求那是刻薄的,我只是对此感到困惑。
  • 因为您打算在浏览器中运行编译为 JS 的咖啡脚本。只要您有一个运行它的浏览器,它就是一种查看值的简单方法。
  • 现在,我会坚持在您的浏览器中运行它。由于很多原因,Node.js(甚至通过coffee 命令)有点复杂。我还建议在开始使用 coffeescript 之前学习基本的 javascript。
  • hpaulj 那是因为它通过浏览器运行
【解决方案2】:

alertprint 不是原生的 node.js 函数

如果您想在不更改示例代码的情况下从命令行开始使用咖啡,sn-ps 在运行代码之前尝试在提示符处执行以下两个分配。

print = console.log
alert = console.log

这里有一个小的 Hello World 函数可以帮助您入门:-

coffee&gt; hello = (word) -&gt; console.log "Hello " + word
coffee&gt; hello "World"
Hello World

【讨论】:

    【解决方案3】:

    在使用“警报”命令之前

    1. 必须安装“alert-node”库
    2. 脚本需要“alert-node”

    点击此链接-> Alert function not working in coffeescript

    npm install alert-node
    
    alert = require('alert-node')

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-21
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      • 1970-01-01
      • 2023-02-07
      • 1970-01-01
      • 2020-11-25
      相关资源
      最近更新 更多