標籤:color require object logs main idt ble tco 9.png
第二章:Node.js中互動式運行環境--------REL
一:REPL運行環境概述
1 C:\Users\junliu>node 2 > foo = ‘bar‘ ; 3 ‘bar‘ 4 >
二:在REPL運行環境中操作變數
1 C:\Users\junliu>node 2 > foo=‘bar‘ 3 ‘bar‘ 4 > var foo=‘bar‘ 5 undefined 6 >
1 console.log("foo=‘bar‘"); //控制台視窗中將輸出“bar” 2 console.log("var foo=‘bar‘");//控制台視窗中將輸出 undefined
1 //為變數賦值 2 > foo=‘bar‘; 3 ‘bar‘ 4 //輸入變數名後顯示變數值 5 > foo 6 ‘bar‘ 7 >
1 //將對象賦值給變數 2 > user =new Object(); 3 {} 4 > user.Name=‘liujun‘; 5 ‘liujun‘ 6 > user.age=‘25‘; 7 ‘25‘ 8 //輸入變數名後顯示變數所引用對象的各屬性名稱及屬性值 9 > user 10 { Name: ‘liujun‘, age: ‘25‘ } 11 >
1 //將對象賦值給變數 2 > user =new Object(); 3 {} 4 > user.Name=‘liujun‘; 5 ‘liujun‘ 6 > user.age=‘25‘; 7 ‘25‘ 8 > user 9 { Name: ‘liujun‘, age: ‘25‘ } 10 //輸入變數後顯示變數所引用對象的各屬性名稱及屬性值,使用“【function】”來顯示函數 11 > user.setName=function(name){user.name=name}; 12 [Function] 13 > user 14 { Name: ‘liujun‘, age: ‘25‘, setName: [Function] } 15 >三:在REPL運行環境中使用底線
1 > a=3; 2 3 3 > _+=1; 4 Expression assignment to _ now disabled. 5 4 6 > a 7 3 8 >
1 > a=3; 2 3 3 > _+=1; 4 Expression assignment to _ now disabled. 5 4 6 > a 7 3 8 >
1 C:\Users\junliu>node 2 > [‘1‘,‘2‘,‘3‘] 3 [ ‘1‘, ‘2‘, ‘3‘ ] 4 > _.length; 5 3 6 > [1,2,3,4,5,5]; 7 [ 1, 2, 3, 4, 5, 5 ] 8 > _.length; 9 6 10 > 6+3; 11 9 12 > _.toString(); 13 ‘9‘ 14 >
四:在REPL運行環境中直接運行函數
以下是在REPL運行環境中運行函數
1 C:\Users\junliu>node 2 > a=[1,1,1,3]; 3 [ 1, 1, 1, 3 ] 4 > a.forEach(function(v){ 5 ... console.log(v); 6 ... }); 7 1 8 1 9 1 10 3
以下為:REPL運行環境將為子函數繼續添加省略符號
1 C:\Users\junliu>node 2 > a=[1,1,2,3,4,5] 3 [ 1, 1, 2, 3, 4, 5 ] 4 > a.forEach(function(v){ 5 ... console.log(v); 6 ... }); 7 1 8 1 9 2 10 3 11 4 12 5 13 undefined 14 > a.forEach(function(v){ 15 ... sf(v); 16 ... function sf(vv){ 17 ..... console.log(vv); 18 ..... } 19 ... }); 20 1 21 1 22 2 23 3 24 4 25 5 26 undefined五:在REPL運行環境中定義並啟動伺服器
1 > var http=require(‘http‘); 2 undefined 3 > http.createServer(function (req,res){ 4 ... res.writeHead(200, {‘Content-Type‘: ‘text/html‘}); 5 ... res.write(‘<head><meta charset="utf-8"/></head>‘); 6 ... res.end(‘你好\n‘); 7 ... }).listen(1337, "127.0.0.1"); 8 Server { 9 domain: 10 Domain { 11 domain: null, 12 _events: { error: [Function] }, 13 _eventsCount: 1, 14 _maxListeners: undefined, 15 members: [] }, 16 _events: 17 { request: [Function], 18 connection: [Function: connectionListener] }, 19 _eventsCount: 2, 20 _maxListeners: undefined, 21 _connections: 0, 22 _handle: null, 23 _usingSlaves: false, 24 _slaves: [], 25 _unref: false, 26 allowHalfOpen: true, 27 pauseOnConnect: false, 28 httpAllowHalfOpen: false, 29 timeout: 120000, 30 _pendingResponseData: 0 } 31 > console.log(‘Server running at http://127.0.0.1:1337/‘) 32 Server running at http://127.0.0.1:1337/ 33 undefined 34 >六:REPL運行環境中的內容物件
1 var repl = require("repl"); 2 var con=repl.start("> ").context; 3 con.msg="樣本訊息"; 4 con.testFunction=function(){console.log(con.msg);};
七:REPL運行環境中的基礎命令
1 > a=[2,3,4,5,6,7]; 2 [ 2, 3, 4, 5, 6, 7 ] 3 > _.length; 4 6 5 > a.forEach(function(v){ 6 ... subF(v); 7 ... function subF(vv){ 8 ..... console.log(vv); 9 ..... } 10 ... }); 11 2 12 3 13 4 14 5 15 6 16 7 17 undefined 18 > a.forEach(function(v){ 19 ... subF(v); 20 ... function subF(vv){ 21 ..... console.log(vv); 22 ..... break; 23 break; 24 ^^^^^ 25 26 SyntaxError: Illegal break statement 27 28 >
按兩次Ctrl+c 退出REPL環境
使用.clear方法清除內容物件中儲存的所有變數和函數
使用 .help 命令顯示所有的基礎命令
1 a=[1,2,3,4,5,6,69,8,7,8,8]; 2 a.forEach(function(v){ 3 sf(v); 4 function sf(vv){ 5 console.log(vv); 6 } 7 });
八:小結
NodeJs>------->>第二章:Node.js中互動式運行環境--------REL