代码之家  ›  专栏  ›  技术社区  ›  ele

从“coffee”可执行文件调用函数

  •  2
  • ele  · 技术社区  · 12 年前

    原谅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
    

    我通过Homebrew安装了Node,通过(全球)安装了CoffeeScript npm .

    3 回复  |  直到 12 年前
        1
  •  5
  •   Alex Wayne    12 年前

    alert 不是 特色 javascript的。它是API浏览器的一部分 暴露 到JavaScript。和 coffee 你电脑上的命令行只是一个薄薄的包装 node.js 将coffee脚本翻译成javascript,由node进行解释。 节点.js 不提供 警觉的 作用它也没有提供一个全球性的 print 作用

    节点和浏览器都提供了 console 全局对象。所以 console.log 工作原理相同。

    刷一下 node docs 以了解功能节点公开了什么。请记住,仅仅因为它在浏览器中工作并不意味着它将在节点中工作。

        2
  •  1
  •   Matthew Slight    9 年前

    alert print 不是本地的 node.js 功能

    如果您想在不更改示例代码片段的情况下从命令行开始喝咖啡,请在运行代码之前,在提示符处尝试以下两项操作。

    print = console.log
    alert = console.log

    这里有一个小的Hello World功能,让您开始使用:-

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

        3
  •  0
  •   Ploychanok Jaleansook    8 年前

    在使用“alert”命令之前

    1. 必须安装“alert node”库
    2. 脚本需要“警报节点”

    关注此链接-> Alert function not working in coffeescript

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