知识共享许可协议
本作品采用知识共享署名-非商业性使用 3.0 未本地化版本许可协议进行许可。

Node.js v0.10.18 手册 & 文档


REPL#

稳定度: 3 - 稳定

A Read-Eval-Print-Loop (REPL) is available both as a standalone program and easily includable in other programs. The REPL provides a way to interactively run JavaScript and see the results. It can be used for debugging, testing, or just trying things out.

一个 Read-Eval-Print-Loop(REPL,读取-执行-输出循环)既可用于独立程序也可很容易地被集成到其它程序中。REPL 提供了一种交互地执行 JavaScript 并查看输出的方式。它可以被用作调试、测试或仅仅尝试某些东西。

By executing node without any arguments from the command-line you will be dropped into the REPL. It has simplistic emacs line-editing.

在命令行中不带任何参数执行 node 您便会进入 REPL。它提供了一个简单的 Emacs 行编辑。

mjr:~$ node
Type '.help' for options.
> a = [ 1, 2, 3];
[ 1, 2, 3 ]
> a.forEach(function (v) {
...   console.log(v);
...   });
1
2
3

For advanced line-editors, start node with the environmental variable NODE_NO_READLINE=1. This will start the main and debugger REPL in canonical terminal settings which will allow you to use with rlwrap.

若想使用高级的编辑模式,设置环境变量 NODE_NO_READLINE=1 后运行 node。这将在允许你在可以使用 rlwrap 的终端上,启动高级的 REPL 模式 (the main and debugger REPL)。

For example, you could add this to your bashrc file:

例如,您可以将下列代码加入到您的 bashrc 文件:

alias node="env NODE_NO_READLINE=1 rlwrap node"

repl.start(options)#

Returns and starts a REPLServer instance. Accepts an "options" Object that takes the following values:

启动并返回一个 REPLServer 实例。接受一个包含如下内容的 "options" 对象:

  • prompt - the prompt and stream for all I/O. Defaults to > .

  • prompt - 所有输入输出的提示符。默认是 > .

  • input - the readable stream to listen to. Defaults to process.stdin.

  • input - 监听的可读流。默认指向标准输入流 process.stdin

  • output - the writable stream to write readline data to. Defaults to process.stdout.

  • output - 用来输出数据的可写流。默认指向标准输出流 process.stdout

  • terminal - pass true if the stream should be treated like a TTY, and have ANSI/VT100 escape codes written to it. Defaults to checking isTTY on the output stream upon instantiation.

  • terminal - 如果 stream 应该被当做 TTY 来对待并且有 ANSI/VT100 转义时,则传 true。 默认使用 output 实例的 isTTY来检查。

  • eval - function that will be used to eval each given line. Defaults to an async wrapper for eval(). See below for an example of a custom eval.

  • eval - 用来对每一行进行求值的函数。 默认为eval()的一个异步包装函数。下面给出一个自定义eval的例子。

  • useColors - a boolean which specifies whether or not the writer function should output colors. If a different writer function is set then this does nothing. Defaults to the repl's terminal value.

  • useColors - 一个布尔值,表明了writer函数是否会输出颜色。如果设定了一个不同的writer函数,那么这不会产生任何影响。默认为repl的terminal值。

  • useGlobal - if set to true, then the repl will use the global object, instead of running scripts in a separate context. Defaults to false.

  • useGlobal - 如果设定为true,那么repl就会使用global对象而不是在一个独立环境里运行脚本。默认为false

  • ignoreUndefined - if set to true, then the repl will not output the return value of command if it's undefined. Defaults to false.

  • ignoreUndefined - 如果设定为true,那么repl将不会输出未定义命令的返回值。默认为false

  • writer - the function to invoke for each command that gets evaluated which returns the formatting (including coloring) to display. Defaults to util.inspect.

  • writer - 每一个命令被求值时都会调用此函数,而该函数会返回显示的格式(包括颜色)。默认为util.inspectutil.inspect.

You can use your own eval function if it has following signature:

你可以使用你自己的eval函数,只有它有如下的签名:

function eval(cmd, context, filename, callback) {
  callback(null, result);
}

Multiple REPLs may be started against the same running instance of node. Each will share the same global object but will have unique I/O.

多个REPL可以在同一个运行的节点实例上打开。它们共享同一个global对象,但分别有各自的I/O。

Here is an example that starts a REPL on stdin, a Unix socket, and a TCP socket:

以下是通过标准输入流(stdin)、Unix socket 以及 TCP socket 三种情况来启动 REPL 的例子:

net.createServer(function (socket) {
  connections += 1;
  repl.start({
    prompt: "node via TCP socket> ",
    input: socket,
    output: socket
  }).on('exit', function() {
    socket.end();
  });
}).listen(5001);

Running this program from the command line will start a REPL on stdin. Other REPL clients may connect through the Unix socket or TCP socket. telnet is useful for connecting to TCP sockets, and socat can be used to connect to both Unix and TCP sockets.

从命令行运行该程序,将会从标准输入流启动 REPL 模式。 其他的 REPL 客户端也可以通过 Unix socket 或者 TCP socket 连接。 telnet 常用于连接 TCP sockets,而 socat 则可以同时用来连接 Unix 和 TCP sockets。

By starting a REPL from a Unix socket-based server instead of stdin, you can connect to a long-running node process without restarting it.

通过从一个Unix的套接字服务器而不是stdin来启动REPL, 你可以连接到一个长久运行的node进程而不不需要重启。

For an example of running a "full-featured" (terminal) REPL over a net.Server and net.Socket instance, see: https://gist.github.com/2209310

一个在net.Servernet.Socket实例上运行的"全功能"(terminal)REPL的例子可以查看这里: https://gist.github.com/2209310

For an example of running a REPL instance over curl(1), see: https://gist.github.com/2053342

一个在curl(1)上运行的REPL实例的例子可以查看这里: https://gist.github.com/2053342

事件: 'exit'#

function () {}

function () {}

Emitted when the user exits the REPL in any of the defined ways. Namely, typing .exit at the repl, pressing Ctrl+C twice to signal SIGINT, or pressing Ctrl+D to signal "end" on the input stream.

当用户通过任意预定义的方式退出REPL,该事件被分发。比如,在repl里输入.exit,按Ctrl+C两次来发送SIGINT信号,或者在input流上按Ctrl+D来发送"end"。

Example of listening for exit:

监听 exit 事件的例子:

r.on('exit', function () {
  console.log('从 REPL 得到 "exit" 事件!');
  process.exit();
});

事件: 'reset'#

function (context) {}

function (context) {}

Emitted when the REPL's context is reset. This happens when you type .clear. If you start the repl with { useGlobal: true } then this event will never be emitted.

当REPL的上下文被重置时,该事件被分发。当你打.clear命令时这种情况就会发生。如果你以{ useGlobal: true }来启动repl,那么这个事件就永远不会被分发。

Example of listening for reset:

监听reset的例子:

// 当一个新的上下文被创建时,扩充这个上下文。
r.on('reset', function (context) {
  console.log('repl有一个新的上下文');
  someExtension.extend(context);
});

REPL 特性#

Inside the REPL, Control+D will exit. Multi-line expressions can be input. Tab completion is supported for both global and local variables.

在REPL里,Control+D会退出。可以输入多行表达式。对于全局变量和本地变量都支持自动缩进。

The special variable _ (underscore) contains the result of the last expression.

特殊变量 _ (下划线)储存了上一个表达式的结果。

> [ "a", "b", "c" ]
[ 'a', 'b', 'c' ]
> _.length
3
> _ += 1
4

The REPL provides access to any variables in the global scope. You can expose a variable to the REPL explicitly by assigning it to the context object associated with each REPLServer. For example:

REPL提供了访问global域里所有变量的权限。通过将一个变量赋值给与每一个REPLServer关联的context对象,你可以显式地将一个变量暴露给REPL。例如:

repl.start("> ").context.m = msg;

Things in the context object appear as local within the REPL:

context对象里的东西,会在REPL以本地变量的形式出现。

mjr:~$ node repl_test.js
> m
'message'

There are a few special REPL commands:

有几个特殊的REPL命令:

  • .break - While inputting a multi-line expression, sometimes you get lost or just don't care about completing it. .break will start over.
  • .clear - Resets the context object to an empty object and clears any multi-line expression.
  • .exit - Close the I/O stream, which will cause the REPL to exit.
  • .help - Show this list of special commands.
  • .save - Save the current REPL session to a file

    .save ./file/to/save.js

  • .load - Load a file into the current REPL session.

    .load ./file/to/load.js

  • .break - 当你输入一个多行表达式时,有时你会脑子突然断路或者你不想完成这个表达式了。break让你可以重头再来。

  • .clear - 重置context对象为一个空对象,并且清除所有的多行表达式。
  • .exit - 关闭I/O流,使得REPL退出。
  • .help - 显示这个特殊命令的列表。
  • .save - 将当前的REPL会话保存到一个文件

    .save ./file/to/save.js

  • .load - 将一个文件装载到当前的REPL会话。

    .load ./file/to/load.js

The following key combinations in the REPL have these special effects:

下面的组合键在REPL中有以下效果:

  • <ctrl>C - Similar to the .break keyword. Terminates the current command. Press twice on a blank line to forcibly exit.
  • <ctrl>D - Similar to the .exit keyword.

  • <ctrl>C - 与.break关键字类似。终止正在执行的命令。在一个空行连按两次会强制退出。

  • <ctrl>D - 与.exit关键字类似。