node. js authoritative Guide, chapter 2nd Interactive Runtime Environment in node. JS (--REPL)
Developers can easily enter various JavaScript expressions and observe the results of the expression in this environment.
In the process of learning the node. JS Framework, we can easily understand the various properties and methods that are owned by various objects defined in node. js through the use of this runtime environment. This section introduces you to operating variables in the REPL runtime environment.
2.2 Manipulating variables in the REPL run environment
In the REPL runtime environment, you can use the var keyword to define a variable and assign it a value, but after you enter an expression for its assignment, the result of the expression is displayed as undefined.
For example, in two expressions like the one shown below, the VAR keyword is not used in the first expression, the result is a variable value, the second expression uses the var keyword, and the result isundefined。
The two different results occur because the Eval function is used internally in the REPL environment to evaluate the execution results of the expression. In JavaScript code, if these two expressions are used as arguments to the Eval function, the Eval function returns different results.
Console.log ("foo= ' Bar"); The console window will output "bar" console.log ("varfoo= ' Bar");
After assigning a value to a variable in the REPL run environment, we can view the variable contents by entering the variable name.
after assigning the value of a simple type to a variable, enter the variable name and press ENTER, the variable value will be displayed directly in the REPL runtime, and the input expression and its execution result are as follows:
Assigning values to variables > foo= ' Bar '; ' Bar ' //input variable name shows variable value >foo ' Bar '
after assigning the object to the variable, enter the variable name and press ENTER, and the REPL run environment will directly display the property names and values of the objects referenced by the variable, and the input expression and its execution result are as follows:
If a property value of the object referenced by the variable is a function, enter the variable name and enter the return key, the REPL run environment will display the property value as "[function]" (this is because the function can often have a lot of content, if the function content is used in the REPL run environment to display the property value, The content of a variable can tend to become very long and long, and the input expression and its execution result are as follows:
2.3 Using the underscore character in the REPL run environment
In the REPL runtime environment, the most recently used expression can be accessed using the underscore character ("_"). For example, after you assign a value of 3 to a variable, you can access the most recent expression by using an underscore character, and the input expression and its execution result are as follows:
However, entering an underscore does not mean that you can modify the value of a variable. For example, after entering an expression that assigns a value of variable A to 3 and the result of an expression to 1 by an underscore character, the value of variable A is displayed immediately after the input expression, and the value of the variable A in the REPL runtime is still 3.
You can use the underscore character to access the properties of the most recent expression execution or to execute a method on it, and the input expression and its execution result are as follows:
2.4 Running functions directly in the REPL operating environment
You can run a function directly in the REPL run environment. In the REPL run environment, you can divide an expression (such as a function in this example) into multiple lines of writing, and when the expression is not completed, the REPL runtime adds an English ellipsis (three dots) for each line of the expression, except for the first row, as shown in the input expression and its execution result 2-3.
In the repl run environment, you can also nest run child functions in the parent function, and when the parent function is divided into multiple lines for writing, the REPL runtime will continue to add English ellipsis (three dots) before each line of code in the child function body.
Interactive runtime Environment in node. JS-REPL