REPL 交互式解释器

http://shouce.w3cfuns.com/nodejs/repl.html

Node.js Manual & Documentation


REPL 交互式解释器

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

交互式解释器(REPL)既能够做为一个独立的程序运行,也能够很容易地包含在其余程序中做为总体程序的一部分使用。REPL为运行JavaScript脚本与查看运行结果提供了一种交互方式,一般REPL交互方式能够用于调试、测试以及试验某种想法。express

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

在命令行中不带任何参数地运行node命令,就能够进入REPL环境,在该环境下你能够进行一些相似Emacs的行编辑操做。app

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 REPL in canonical terminal settings which will allow you to use withrlwrap.socket

为了进行高级的行编辑操做,能够设置环境变量NODE_NO_READLINE=1并启动node。这种状况REPL会进入标准终端设置模式,这此模式下你可使用rlwrapide

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

好比,你能够把下列设置添加到你的bashrc文件中:测试

alias node="env NODE_NO_READLINE=1 rlwrap node"

repl.start(prompt='> ', stream=process.stdin)

Starts a REPL with prompt as the prompt and stream for all I/O. prompt is optional and defaults to stream is optional and defaults to process.stdin.ui

启动一个REPL,使用prompt做为输入提示符,在stream上进行全部I/O操做。prompt是可选参数,默认值为stream也是可选参数,默认值为process.stdin

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.

一个node实例中能够启动多个REPL,它们共享相同的全局对象,但拥有各自独立的I/O。

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

下面是的例子展现分别在标准输入,Unix套接字及TCP套接字上启动的REPL示例:

var net = require("net"), repl = require("repl"); connections = 0; repl.start("node via stdin> "); net.createServer(function (socket) { connections += 1; repl.start("node via Unix socket> ", socket); }).listen("/tmp/node-repl-sock"); net.createServer(function (socket) { connections += 1; repl.start("node via TCP socket> ", socket); }).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套接字或者TCP套接字进行链接。可以使用telnet程序链接到TCP套接字,而socat程序既能够链接到Unix套接字也能够链接链接到TCP套接字。

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.

若要链接到一个长时间运行的node进程而无需重启进程,你应该将REPL启动在Unix套接字上,非不要将其启动在标准输出上。

REPL Features REPL特性

Inside the REPL, Control+D will exit. Multi-line expressions can be input.

在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 eachREPLServer. For example:

在REPL能够访问全局做用域中的任何变量。为了在REPL中访问一个变量,你只要将此变量显性地分配给相应REPLServercontext对象。示例以下:

// repl_test.js var repl = require("repl"), msg = "message"; 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. .break - 当你输入多行表达式,若是想放弃当前的输入,能够用.break跳出。
  • .clear - Resets the context object to an empty object and clears any multi-line expression. .clear - 将context重置为空对象,并清空当前正在输入的多行表达式。
  • .exit - Close the I/O stream, which will cause the REPL to exit. .exit - 该命令用于关闭I/O流,并退出REPL。
  • .help - Show this list of special commands. .help - 输出特殊命令的列表。
相关文章
相关标签/搜索