{
  "source": "repl.markdown",
  "modules": [
    {
      "textRaw": "REPL",
      "name": "repl",
      "desc": "<pre><code>稳定度: 3 - 稳定</code></pre>\n<p>A Read-Eval-Print-Loop (REPL) is available both as a standalone program and\neasily includable in other programs. The REPL provides a way to interactively\nrun JavaScript and see the results.  It can be used for debugging, testing, or\njust trying things out.\n\n</p>\n<p>一个 Read-Eval-Print-Loop（REPL，读取-执行-输出循环）既可用于独立程序也可很容易地被集成到其它程序中。REPL 提供了一种交互地执行 JavaScript 并查看输出的方式。它可以被用作调试、测试或仅仅尝试某些东西。\n\n</p>\n<p>By executing <code>node</code> without any arguments from the command-line you will be\ndropped into the REPL. It has simplistic emacs line-editing.\n\n</p>\n<p>在命令行中不带任何参数执行 <code>node</code> 您便会进入 REPL。它提供了一个简单的 Emacs 行编辑。\n\n</p>\n<pre><code>mjr:~$ node\nType &apos;.help&apos; for options.\n&gt; a = [ 1, 2, 3];\n[ 1, 2, 3 ]\n&gt; a.forEach(function (v) {\n...   console.log(v);\n...   });\n1\n2\n3</code></pre>\n<p>For advanced line-editors, start node with the environmental variable\n<code>NODE_NO_READLINE=1</code>. This will start the main and debugger REPL in canonical\nterminal settings which will allow you to use with <code>rlwrap</code>.\n\n</p>\n<p>若想使用高级的编辑模式，设置环境变量 <code>NODE_NO_READLINE=1</code> 后运行 node。这将在允许你在可以使用 <code>rlwrap</code> 的终端上，启动高级的 REPL 模式 (the main and debugger REPL)。\n\n</p>\n<p>For example, you could add this to your bashrc file:\n\n</p>\n<p>例如，您可以将下列代码加入到您的 bashrc 文件：\n\n</p>\n<pre><code>alias node=&quot;env NODE_NO_READLINE=1 rlwrap node&quot;</code></pre>\n",
      "methods": [
        {
          "textRaw": "repl.start(options)",
          "type": "method",
          "name": "start",
          "desc": "<p>Returns and starts a <code>REPLServer</code> instance. Accepts an &quot;options&quot; Object that\ntakes the following values:\n\n</p>\n<p>启动并返回一个 <code>REPLServer</code> 实例。接受一个包含如下内容的 &quot;options&quot; 对象：\n\n</p>\n<ul>\n<li><p><code>prompt</code> - the prompt and <code>stream</code> for all I/O. Defaults to <code>&gt; </code>.</p>\n</li>\n<li><p><code>prompt</code> - 所有输入输出的提示符。默认是 <code>&gt; </code>.</p>\n</li>\n<li><p><code>input</code> - the readable stream to listen to. Defaults to <code>process.stdin</code>.</p>\n</li>\n<li><p><code>input</code> - 监听的可读流。默认指向标准输入流 <code>process.stdin</code>。</p>\n</li>\n<li><p><code>output</code> - the writable stream to write readline data to. Defaults to\n<code>process.stdout</code>.</p>\n</li>\n<li><p><code>output</code> - 用来输出数据的可写流。默认指向标准输出流 <code>process.stdout</code>。</p>\n</li>\n<li><p><code>terminal</code> - pass <code>true</code> if the <code>stream</code> should be treated like a TTY, and\nhave ANSI/VT100 escape codes written to it. Defaults to checking <code>isTTY</code>\non the <code>output</code> stream upon instantiation.</p>\n</li>\n<li><p><code>terminal</code> - 如果 <code>stream</code> 应该被当做 TTY 来对待并且有 ANSI/VT100 转义时，则传 <code>true</code>。 默认使用 <code>output</code> 实例的 <code>isTTY</code>来检查。</p>\n</li>\n<li><p><code>eval</code> - function that will be used to eval each given line. Defaults to\nan async wrapper for <code>eval()</code>. See below for an example of a custom <code>eval</code>.</p>\n</li>\n<li><p><code>eval</code> - 用来对每一行进行求值的函数。 默认为<code>eval()</code>的一个异步包装函数。下面给出一个自定义<code>eval</code>的例子。</p>\n</li>\n<li><p><code>useColors</code> - a boolean which specifies whether or not the <code>writer</code> function\nshould output colors. If a different <code>writer</code> function is set then this does\nnothing. Defaults to the repl&apos;s <code>terminal</code> value.</p>\n</li>\n<li><p><code>useColors</code> - 一个布尔值，表明了<code>writer</code>函数是否会输出颜色。如果设定了一个不同的<code>writer</code>函数，那么这不会产生任何影响。默认为repl的<code>terminal</code>值。</p>\n</li>\n<li><p><code>useGlobal</code> - if set to <code>true</code>, then the repl will use the <code>global</code> object,\ninstead of running scripts in a separate context. Defaults to <code>false</code>.</p>\n</li>\n<li><p><code>useGlobal</code> - 如果设定为<code>true</code>，那么repl就会使用<code>global</code>对象而不是在一个独立环境里运行脚本。默认为<code>false</code>。</p>\n</li>\n<li><p><code>ignoreUndefined</code> - if set to <code>true</code>, then the repl will not output the\nreturn value of command if it&apos;s <code>undefined</code>. Defaults to <code>false</code>.</p>\n</li>\n<li><p><code>ignoreUndefined</code> - 如果设定为<code>true</code>，那么repl将不会输出未定义命令的返回值。默认为<code>false</code>。</p>\n</li>\n<li><p><code>writer</code> - the function to invoke for each command that gets evaluated which\nreturns the formatting (including coloring) to display. Defaults to\n<code>util.inspect</code>.</p>\n</li>\n<li><p><code>writer</code> - 每一个命令被求值时都会调用此函数，而该函数会返回显示的格式（包括颜色）。默认为<code>util.inspect</code>。\n<code>util.inspect</code>.</p>\n</li>\n</ul>\n<p>You can use your own <code>eval</code> function if it has following signature:\n\n</p>\n<p>你可以使用你自己的<code>eval</code>函数，只有它有如下的签名：\n\n</p>\n<pre><code>function eval(cmd, context, filename, callback) {\n  callback(null, result);\n}</code></pre>\n<p>Multiple REPLs may be started against the same running instance of node.  Each\nwill share the same global object but will have unique I/O.\n\n</p>\n<p>多个REPL可以在同一个运行的节点实例上打开。它们共享同一个global对象，但分别有各自的I/O。\n\n</p>\n<p>Here is an example that starts a REPL on stdin, a Unix socket, and a TCP socket:\n\n</p>\n<p>以下是通过标准输入流（stdin）、Unix socket 以及 TCP socket 三种情况来启动 REPL 的例子:\n\n</p>\n<pre><code>net.createServer(function (socket) {\n  connections += 1;\n  repl.start({\n    prompt: &quot;node via TCP socket&gt; &quot;,\n    input: socket,\n    output: socket\n  }).on(&apos;exit&apos;, function() {\n    socket.end();\n  });\n}).listen(5001);</code></pre>\n<p>Running this program from the command line will start a REPL on stdin.  Other\nREPL clients may connect through the Unix socket or TCP socket. <code>telnet</code> is useful\nfor connecting to TCP sockets, and <code>socat</code> can be used to connect to both Unix and\nTCP sockets.\n\n</p>\n<p>从命令行运行该程序，将会从标准输入流启动 REPL 模式。 其他的 REPL 客户端也可以通过 Unix socket 或者 TCP socket 连接。 <code>telnet</code> 常用于连接 TCP sockets，而 <code>socat</code> 则可以同时用来连接 Unix 和 TCP sockets。\n\n</p>\n<p>By starting a REPL from a Unix socket-based server instead of stdin, you can\nconnect to a long-running node process without restarting it.\n\n</p>\n<p>通过从一个Unix的套接字服务器而不是stdin来启动REPL, 你可以连接到一个长久运行的node进程而不不需要重启。\n\n</p>\n<p>For an example of running a &quot;full-featured&quot; (<code>terminal</code>) REPL over\na <code>net.Server</code> and <code>net.Socket</code> instance, see: <a href=\"https://gist.github.com/2209310\">https://gist.github.com/2209310</a>\n\n</p>\n<p>一个在<code>net.Server</code>和<code>net.Socket</code>实例上运行的&quot;全功能&quot;(<code>terminal</code>)REPL的例子可以查看这里： <a href=\"https://gist.github.com/2209310\">https://gist.github.com/2209310</a>\n\n</p>\n<p>For an example of running a REPL instance over <code>curl(1)</code>,\nsee: <a href=\"https://gist.github.com/2053342\">https://gist.github.com/2053342</a>\n\n</p>\n<p>一个在<code>curl(1)</code>上运行的REPL实例的例子可以查看这里： <a href=\"https://gist.github.com/2053342\">https://gist.github.com/2053342</a>\n\n</p>\n",
          "modules": [
            {
              "textRaw": "事件: 'exit'",
              "name": "事件:_'exit'",
              "desc": "<p><code>function () {}</code>\n\n</p>\n<p><code>function () {}</code>\n\n</p>\n<p>Emitted when the user exits the REPL in any of the defined ways. Namely, typing\n<code>.exit</code> at the repl, pressing Ctrl+C twice to signal SIGINT, or pressing Ctrl+D\nto signal &quot;end&quot; on the <code>input</code> stream.\n\n</p>\n<p>当用户通过任意预定义的方式退出REPL，该事件被分发。比如，在repl里输入<code>.exit</code>，按Ctrl+C两次来发送SIGINT信号，或者在<code>input</code>流上按Ctrl+D来发送&quot;end&quot;。\n\n</p>\n<p>Example of listening for <code>exit</code>:\n\n</p>\n<p>监听 <code>exit</code> 事件的例子：\n\n</p>\n<pre><code>r.on(&apos;exit&apos;, function () {\n  console.log(&apos;从 REPL 得到 &quot;exit&quot; 事件！&apos;);\n  process.exit();\n});</code></pre>\n",
              "type": "module",
              "displayName": "事件: 'exit'"
            },
            {
              "textRaw": "事件: 'reset'",
              "name": "事件:_'reset'",
              "desc": "<p><code>function (context) {}</code>\n\n</p>\n<p><code>function (context) {}</code>\n\n</p>\n<p>Emitted when the REPL&apos;s context is reset. This happens when you type <code>.clear</code>.\nIf you start the repl with <code>{ useGlobal: true }</code> then this event will never\nbe emitted.\n\n</p>\n<p>当REPL的上下文被重置时，该事件被分发。当你打<code>.clear</code>命令时这种情况就会发生。如果你以<code>{ useGlobal: true }</code>来启动repl，那么这个事件就永远不会被分发。\n\n</p>\n<p>Example of listening for <code>reset</code>:\n\n</p>\n<p>监听<code>reset</code>的例子：\n\n</p>\n<pre><code>// 当一个新的上下文被创建时，扩充这个上下文。\nr.on(&apos;reset&apos;, function (context) {\n  console.log(&apos;repl有一个新的上下文&apos;);\n  someExtension.extend(context);\n});</code></pre>\n",
              "type": "module",
              "displayName": "事件: 'reset'"
            }
          ],
          "signatures": [
            {
              "params": [
                {
                  "name": "options"
                }
              ]
            }
          ]
        }
      ],
      "miscs": [
        {
          "textRaw": "REPL 特性",
          "name": "REPL 特性",
          "type": "misc",
          "desc": "<p>Inside the REPL, Control+D will exit.  Multi-line expressions can be input.\nTab completion is supported for both global and local variables.\n\n</p>\n<p>在REPL里，Control+D会退出。可以输入多行表达式。对于全局变量和本地变量都支持自动缩进。\n\n</p>\n<p>The special variable <code>_</code> (underscore) contains the result of the last expression.\n\n</p>\n<p>特殊变量 <code>_</code> (下划线)储存了上一个表达式的结果。\n\n</p>\n<pre><code>&gt; [ &quot;a&quot;, &quot;b&quot;, &quot;c&quot; ]\n[ &apos;a&apos;, &apos;b&apos;, &apos;c&apos; ]\n&gt; _.length\n3\n&gt; _ += 1\n4</code></pre>\n<p>The REPL provides access to any variables in the global scope. You can expose\na variable to the REPL explicitly by assigning it to the <code>context</code> object\nassociated with each <code>REPLServer</code>.  For example:\n\n</p>\n<p>REPL提供了访问global域里所有变量的权限。通过将一个变量赋值给与每一个<code>REPLServer</code>关联的<code>context</code>对象，你可以显式地将一个变量暴露给REPL。例如：\n\n</p>\n<pre><code>repl.start(&quot;&gt; &quot;).context.m = msg;</code></pre>\n<p>Things in the <code>context</code> object appear as local within the REPL:\n\n</p>\n<p>在<code>context</code>对象里的东西，会在REPL以本地变量的形式出现。\n\n</p>\n<pre><code>mjr:~$ node repl_test.js\n&gt; m\n&apos;message&apos;</code></pre>\n<p>There are a few special REPL commands:\n\n</p>\n<p>有几个特殊的REPL命令：\n\n</p>\n<ul>\n<li><code>.break</code> - While inputting a multi-line expression, sometimes you get lost\nor just don&apos;t care about completing it. <code>.break</code> will start over.</li>\n<li><code>.clear</code> - Resets the <code>context</code> object to an empty object and clears any\nmulti-line expression.</li>\n<li><code>.exit</code> - Close the I/O stream, which will cause the REPL to exit.</li>\n<li><code>.help</code> - Show this list of special commands.</li>\n<li><code>.save</code> - Save the current REPL session to a file<blockquote>\n<p>.save ./file/to/save.js</p>\n</blockquote>\n</li>\n<li><p><code>.load</code> - Load a file into the current REPL session.</p>\n<blockquote>\n<p>.load ./file/to/load.js</p>\n</blockquote>\n</li>\n<li><p><code>.break</code> - 当你输入一个多行表达式时，有时你会脑子突然断路或者你不想完成这个表达式了。<code>break</code>让你可以重头再来。</p>\n</li>\n<li><code>.clear</code> - 重置<code>context</code>对象为一个空对象，并且清除所有的多行表达式。</li>\n<li><code>.exit</code> - 关闭I/O流，使得REPL退出。</li>\n<li><code>.help</code> - 显示这个特殊命令的列表。</li>\n<li><code>.save</code> - 将当前的REPL会话保存到一个文件<blockquote>\n<p>.save ./file/to/save.js</p>\n</blockquote>\n</li>\n<li><code>.load</code> - 将一个文件装载到当前的REPL会话。<blockquote>\n<p>.load ./file/to/load.js</p>\n</blockquote>\n</li>\n</ul>\n<p>The following key combinations in the REPL have these special effects:\n\n</p>\n<p>下面的组合键在REPL中有以下效果：\n\n</p>\n<ul>\n<li><code>&lt;ctrl&gt;C</code> - Similar to the <code>.break</code> keyword.  Terminates the current\ncommand.  Press twice on a blank line to forcibly exit.</li>\n<li><p><code>&lt;ctrl&gt;D</code> - Similar to the <code>.exit</code> keyword.</p>\n</li>\n<li><p><code>&lt;ctrl&gt;C</code> - 与<code>.break</code>关键字类似。终止正在执行的命令。在一个空行连按两次会强制退出。</p>\n</li>\n<li><code>&lt;ctrl&gt;D</code> - 与<code>.exit</code>关键字类似。</li>\n</ul>\n"
        }
      ],
      "type": "module",
      "displayName": "REPL"
    }
  ]
}