从`coffee`可执行文件调用函数

时间:2013-10-03 21:53:37

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

请原谅noob问题,但为什么我不能(几乎)调用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

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

3 个答案:

答案 0 :(得分:5)

alert不是javascript的功能。它是浏览器公开到JavaScript的API的一部分。并且coffee在您的计算机上的命令行只是node.js的一个薄包装器,它将咖啡脚本转换为javascript以便由节点解释。 node.js未提供alert功能。它也不提供全局print功能。

节点和浏览器都会在全局范围内提供console对象。所以console.log的工作方式相同。

刷新node docs以了解节点暴露的功能。并且记住,仅仅因为它在浏览器中有效并不意味着它在节点中起作用。

答案 1 :(得分:1)

alertprint不是原生node.js函数

如果您想在命令行中使用咖啡而不更改示例代码段,请在运行代码之前在提示符处尝试以下两项分配。

print = console.log
alert = console.log

这是一个小型的Hello World功能,可以帮助您入门: -

coffee> hello = (word) -> console.log "Hello " + word
coffee> hello "World"
Hello World

答案 2 :(得分:0)

使用'alert'命令之前

  1. 必须安装'alert-node'lib
  2. 要求'alert-node'到您的脚本
  3. 点击此链接 - &gt; Alert function not working in coffeescript

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

相关问题