Node.js Learning Interactive Interpreter REPL detailed _node.js

Source: Internet
Author: User
Tags eval

Brief introduction

REPL is a read-eval-print-loop (REPL, read-execute-output-loop) implementation provided by Node.js, which can be used as a stand-alone program and can be included in other applications. REPL is an interop command-line parser that provides an interactive programming environment that validates the code you write in real time and is ideal for validating node.js and JavaScript related APIs.

Node has its own interactive interpreter, which can perform the following tasks:

Read-read user input, parse input JavaScript data structure and store in memory.

Execution-Performs the input data structure

Print-Output results

Loop-loop operation the above steps until the user presses the Ctrl-c button two times to exit.

The interactive interpreter for Node can be a good debugging of Javascript code.

REPL can be used as a stand-alone stand-alone program, or it can be included in other programs.

It provides an interactive way of "executing programs, showing results."

It can be used as a debugging,testing or just perform an operation to get some results.

Execute REPL

Open the command line, reach the directory where the Node.js is installed, and type node

$ node

You will see the REPL command prompt > Here you can enter any Node.js command.

1), Simple expression

Let's try to do a simple math calculation at Node.js's REPL command prompt:

$ node
> 2 + 3
> 2 + (2 * 3)-4
>

Note: > is the prompt for the REPL command.

2), the use of variable

You can store the data in a variable and use it when you need it.

Variable declarations require the use of the var keyword if the var keyword variable is not used to print directly.

Variables that use the VAR keyword can be used console.log() to output variables.

$ node
> x = ten
> var y = ten
undefined
> x + y
> Console.log ("Hello World")
Hell O World
undefined

3), multiple lines of expression

Node REPL supports the input of multiline expressions, which is somewhat like JavaScript.

Next, let's perform a do-while loop:

$ node
> var x = 0
undefined
> do {
... x + +;
... console.log ("x:" + x);
... } while (x < 5);
X:1
x:2
x:3
x:4
x:5
undefined
>

... The symbols of the three dots are automatically generated by the system, and you can return the line after you have changed. Node automatically detects whether it is a contiguous expression.

4), use function

> var name= "aaa"
undefined
> name
' aaa '
> Function getName () {
... console.log ( THIS.name);
... }
Undefined
> GetName ()
aaa
undefined

Because the Eval function is used internally by the REPL environment to evaluate the execution result of the expression, there are some things that we can write directly, such as an object:

> {a:1,b:2}
{a:1, b:2}

> [1,2,3,4,5]
[1, 2, 3, 4, 5]

5), Underline _

Use _ to refer to a value that is executed after the last operation, such as

Object:

> {a:2,b:3}
{a:2, b:3}
> for (var key in _) {
... console.log ("key=" +key+ ", value=" +_[key]);
... }
key=a,value=2
key=b,value=3
undefined

Array:

> {a:2,b:3}
{a:2, b:3}
> for (var key in _) {
... console.log ("key=" +key+ ", value=" +_[key]);
. }
key=a,value=2
key=b,value=3
undefined

The correct result:

> [1,2,3,4,5]
[1, 2, 3, 4, 5]   //array
> Object.keys (_). Map (function (k) {return _[k]*_[k]})
[1, 4 , 9/A]   /element value

3, Repl command

CTRL + C-terminate the current command

CTRL + C twice-terminate node REPL

Ctrl + D-Terminate node REPL

up/down Keys-View command history and modify previous command

tab  Keys-List of current directives help

-list break for all commands

-exit multiline expression clear

-exit save filename from multiline

-current node Repl session saved to file

load FileName-Loads the contents of the file at the current node Repl session

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.