什么是node repl_什么是节点js repl及其用法

什么是node repl

REPL stands for Read Eval Print Loop and it represents a computer environment like a Windows console or Unix/Linux shell where a command is entered and the system responds with an output in an interactive mode. Node.js or Node comes bundled with a REPL environment. It performs the following tasks(

REPL代表Read Eval Print Loop,它代表计算机环境(例如Windows控制台或Unix / Linux shell),在其中输入命令,系统以交互方式响应输出。 Node.js或Node与REPL环境捆绑在一起。 它执行以下任务(

REPL的用途是什么? (What are the usages of REPL?)

  • Debug

    除错

  • execute code

    执行代码

我如何开始使用REPL?(How can I start working with REPL?)

All you need is node on your system. (Installation Guide)

您所需要的只是系统上的node 。 (安装指南)

Now if run node command you can get into REPL:

现在,如果运行node命令,您可以进入REPL:

Image for post
You can run javascript codes in REPL
您可以在REPL中运行javascript代码

REPL代表什么?(What does REPL stand for?)

  • Read

  • Eval

    评估
  • Print

    打印
  • Loop

    循环

When we write a command and press enter REPL reads our command, execute it and print the result and again it is waiting for another command(this is why it has a loop in its definition).

当我们编写命令并按Enter键时,REPL会读取我们的命令,执行该命令并打印结果,然后它再次等待另一个命令(这就是为什么它的定义中包含循环)。

For example, we want to test a condition like '1' == 1 . We write this command and after execution and print CLI is ready for the next command:

例如,我们要测试类似'1' == 1 。 我们编写此命令,并在执行后并打印CLI准备下一条命令:

Image for post

自动完成选项 (Auto-Completion Option)

In REPL you can complete your commands using Tab. for example if we want to see functions and modules inside the global object we can write global. and after that, we can see all global objects using the Tab key twice.

在REPL中,您可以使用Tab键完成命令 例如,如果我们要查看全局对象中的功能和模块,可以编写global. 然后,我们可以使用Tab键两次查看所有全局对象。

NodeJS REPL Auto-Completion Option

上一个命令选项 (Previous Commands Option)

In this command line, you can access your previous commands using up and down arrows. Also, we can access to previous evaluated value using _ :

在此命令行中,您可以使用向上和向下箭头访问以前的命令。 另外,我们可以使用_来访问以前的评估值:

Image for post

节点REPL特殊命令 (Node REPL special commands)

Special command begin with . and we can see a list of them using the Tab key and you can see a description of these commands using .help

特殊命令以开头. 我们可以使用Tab键查看它们的列表,还可以使用.help查看这些命令的说明。

Node REPL special commands
Node Special Commands
节点特殊命令

.editor mode you can write a multiple-line code and you can finish your code using ctrl + D :

.editor模式,您可以编写多行代码,并可以使用ctrl + D完成代码:

NodeJS REPL editor mode
.editor mode
.editor模式

You may notice that we after calling sayHello function I received an undefined and the reason is that I didn’t return anything from this function and REPL logs return value by default. Also when you use some commands without any return value it logs an undefined for example assigning value to a variable doesn’t return anything:

您可能会注意到,我们在调用sayHello函数后收到一个undefined ,原因是我没有从此函数返回任何内容,并且REPL默认记录了返回值。 同样,当您使用一些没有任何返回值的命令时,它会记录一个undefined的值,例如,将值分配给变量不会返回任何内容:

Image for post

.exit Does what it says!

.exit它的意思!

.load you can load an external javascript file into your REPL. example:

.load您可以将外部javascript文件加载到REPL中。 例:

Image for post

At first, I needed to get out of REPL to create a file. I used .exit special command and after that, I created a simple js file with only one line of code: const Pi=3.14 then in REPL, I used .load command to import this file into my REPL session and after that, I have access to this file’s variables and functions as you can see.

首先,我需要退出REPL来创建文件。 我使用.exit特殊命令,然后,我只用一行代码创建了一个简单的js文件: const Pi=3.14然后在REPL中,我使用.load命令将该文件导入到我的REPL会话中,然后,我有了您可以看到该文件的变量和功能。

.save help us saving our session and it’s history into a file for future use. an easy example:

.save帮助我们将会话及其历史保存到文件中以备将来使用。 一个简单的例子:

Image for post

I have assigned two variables and I saved them into a file named mySession and after I loaded it using .load command.

我分配了两个变量,并将它们保存到名为mySession的文件中,并在使用.load命令加载它之后。

.break and .break can get you out of a copy-paste multi-line session.

.break.break可以使您脱离复制粘贴的多行会话。

创建您的自定义REPL (Create Your Custom REPL)

We can create custom REPL using built-in repl module simply like this:First create a js file like: myREPL.js

我们可以使用内置repl模块创建自定义REPL,就像这样:首先创建一个js文件,例如:myREPL.js

Image for post
Creating custom REPL
创建自定义REPL

If you are using VSCODE you can access to all REPL options by using ctrl + space . Now if we run this file with node: node myREPL.js we can run commands in our custom REPL:

如果使用的是VSCODE ,则可以使用ctrl + space访问所有REPL选项。 现在,如果我们使用node: node myREPL.js运行此文件,则可以在自定义REPL中运行命令:

Image for post

As you can see there is no undefined cause I passed an option on line four of myREPL.js

如您所见,没有undefined原因,我在myREPL.js行传递了一个选项

翻译自: https://medium.com/@p.shaddel/what-is-node-js-repl-and-its-usage-26e592da92fb

什么是node repl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值