mac node repl_如何使用Node.js REPL

mac node repl

The node command is the one we use to run our Node.js scripts:

node命令是我们用来运行Node.js脚本的命令:

node script.js

If we omit the filename, we use it in REPL mode:

如果省略文件名,则在REPL模式下使用它:

node

If you try it now in your terminal, this is what happens:

如果您现在在终端中尝试,将发生以下情况:

❯ node
>

the command stays in idle mode and waits for us to enter something.

该命令将保持空闲状态,并等待我们输入内容。

Tip: if you are unsure how to open your terminal, google “How to open terminal on ”.

提示:如果不确定如何打开终端,请在“ ”。

The REPL is waiting for us to enter some JavaScript code, to be more precise.

确切地说,REPL等待我们输入一些JavaScript代码。

Start simple and enter

从简单开始并输入

> console.log('test')
test
undefined
>

The first value, test, is the output we told the console to print, then we get undefined which is the return value of running console.log().

第一个值test是我们告诉控制台打印的输出,然后我们得到undefined,它是正在运行的console.log()的返回值。

We can now enter a new line of JavaScript.

现在,我们可以输入一行新JavaScript。

使用标签自动完成 (Use the tab to autocomplete)

The cool thing about the REPL is that it’s interactive.

REPL的优点在于它是交互式的。

As you write your code, if you press the tab key the REPL will try to autocomplete what you wrote to match a variable you already defined or a predefined one.

在编写代码时,如果按tab键,REPL将尝试自动完成所写内容,以匹配已定义的变量或预定义的变量。

探索JavaScript对象 (Exploring JavaScript objects)

Try entering the name of a JavaScript class, like Number, add a dot and press tab.

尝试输入JavaScript类的名称,例如Number ,添加一个点并按tab

The REPL will print all the properties and methods you can access on that class:

REPL将打印您可以在该类上访问的所有属性和方法:

Pressing tab reveals object properties

探索全局对象 (Explore global objects)

You can inspect the globals you have access to by typing global. and pressing tab:

您可以通过键入global.来检查您可以访问的全局变量global. 然后按tab

Globals

_特殊变量 (The _ special variable)

If after some code you type _, that is going to print the result of the last operation.

如果在某些代码后键入_ ,则将打印最后一个操作的结果。

点命令 (Dot commands)

The REPL has some special commands, all starting with a dot .. They are

REPL有一些特殊的命令,所有命令都以点开头. 。 他们是

  • .help: shows the dot commands help

    .help :显示点命令帮助

  • .editor: enables editor more, to write multiline JavaScript code with ease. Once you are in this mode, enter ctrl-D to run the code you wrote.

    .editor :使编辑者更容易编写多行JavaScript代码。 进入此模式后,输入ctrl-D运行您编写的代码。

  • .break: when inputting a multi-line expression, entering the .break command will abort further input. Same as pressing ctrl-C.

    .break :输入多行表达式时,输入.break命令将中止进一步的输入。 与按Ctrl-C相同。

  • .clear: resets the REPL context to an empty object and clears any multi-line expression currently being input.

    .clear :将REPL上下文重置为空对象,并清除当前正在输入的任何多行表达式。

  • .load: loads a JavaScript file, relative to the current working directory

    .load :相对于当前工作目录加载一个JavaScript文件

  • .save: saves all you entered in the REPL session to a file (specify the filename)

    .save :将您在REPL会话中输入的所有内容保存到文件中(指定文件名)

  • .exit: exists the repl (same as pressing ctrl-C two times)

    .exit :存在.exit (与按两次Ctrl-C相同)

The REPL knows when you are typing a multi-line statement without the need to invoke .editor.

REPL知道何时键入多行语句,而无需调用.editor

For example if you start typing an iteration like this:

例如,如果您开始输入这样的迭代:

[1, 2, 3].forEach(num => {

and you press enter, the REPL will go to a new line that starts with 3 dots, indicating you can now continue to work on that block.

然后按enter键,REPL将转到以3个点开头的新行,表示您现在可以继续在该块上工作。

... console.log(num)
... })

If you type .break at the end of a line, the multiline mode will stop and the statement will not be executed.

如果您在一行的末尾键入.break ,则多行模式将停止并且该语句将不会执行。

翻译自: https://flaviocopes.com/node-repl/

mac node repl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值