/**PageBeginHtml Block Begin **/ /***自定义返回顶部小火箭***/ /*生成博客目录的JS 开始*/ /*生成博客目录的JS 结束*/

NodeJs>------->>第二章:Node.js中交互式运行环境--------REL


第二章:Node.js中交互式运行环境--------REL

                                      

    image

一:REPL运行环境概述

image

imageimage

  1 C:\Users\junliu>node
  2 > foo = 'bar' ;
  3 'bar'
  4 >

image

image

image

二:在REPL运行环境中操作变量


image

  1 C:\Users\junliu>node
  2 > foo='bar'
  3 'bar'
  4 > var foo='bar'
  5 undefined
  6 > 

image

  1 console.log("foo='bar'"); //控制台窗口中将输出“bar”
  2 console.log("var foo='bar'");//控制台窗口中将输出 undefined

image

  1 //为变量赋值
  2 > foo='bar';
  3 'bar'
  4 //输入变量名后显示变量值
  5 > foo
  6 'bar'
  7 >
  

image

  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 >

image

  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运行环境中使用下划线字符

image

  1 > a=3;
  2 3
  3 > _+=1;
  4 Expression assignment to _ now disabled.
  5 4
  6 > a
  7 3
  8 >

image

  1 > a=3;
  2 3
  3 > _+=1;
  4 Expression assignment to _ now disabled.
  5 4
  6 > a
  7 3
  8 >

image

  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运行环境中直接运行函数

image

以下是在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

imageimage

以下为: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运行环境中定义并启动服务器

image

image

  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运行环境中的上下文对象

image

image

  1 var repl = require("repl");
  2 var con=repl.start("> ").context;
  3 con.msg="示例消息";
  4 con.testFunction=function(){console.log(con.msg);};

image

七:REPL运行环境中的基础命令

image

image

image

  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 >

imageimage

image按两次Ctrl+c 退出REPL环境

image使用.clear方法清除上下文对象中保存的所有变量和函数

image

image

image

image

image

image使用 .help 命令显示所有的基础命令

image

image

image

image

  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 });

image

image

八:小结

image


















posted @ 2017-09-07 23:57  一品堂.技术学习笔记  阅读(425)  评论(0编辑  收藏  举报