REPL(Read Eval Print Loop:交互式解釋器)概念
表示一個電腦的環(huán)境,類似 Window 系統(tǒng)的終端或 Unix/Linux shell,我們可以在終端中輸入命令,并接收系統(tǒng)的響應(yīng)。
執(zhí)行步驟:
1、讀取 - 讀取用戶輸入,解析輸入了Javascript 數(shù)據(jù)結(jié)構(gòu)并存儲在內(nèi)存中。
2、執(zhí)行 - 執(zhí)行輸入的數(shù)據(jù)結(jié)構(gòu)
3、打印 - 輸出結(jié)果
4、循環(huán) - 循環(huán)操作以上步驟直到用戶兩次按下 ctrl-c 按鈕退出。
5、進入repl方式:終端中鍵入node
-
表達式運算
$ node > 1 +4 5 > 5 / 2 2.5 > 3 * 6 18 > 4 - 1 3 > 1 + ( 2 * 3 ) - 4 3 > -
使用變量
$ node > x = 10 10 > var y = 10 undefined > x + y 20 > console.log(“Hello World”) Hello World undefined -
多行表達式
$ 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 > -
下劃線
你可以使用下劃線(_)獲取表達式的運算結(jié)果$ node > var x = 10 undefined > var y = 20 undefined > x + y 30 > var sum = _ undefined > console.log(sum) 30 undefined > -
REPL 命令
ctrl + c - 退出當(dāng)前終端。 ctrl + c 按下兩次 - 退出 Node REPL。 ctrl + d - 退出 Node REPL. 向上/向下 鍵 - 查看輸入的歷史命令 tab 鍵 - 列出當(dāng)前命令 .help - 列出使用命令 .break - 退出多行表達式 .clear - 退出多行表達式