nodejs系列(二)REPL交互解释 事件循环
一、REPL交互解释
命令行中输入node启动REPL:
> var x =2;
undefined
> do{x++;
... console.log("x:="+x);
... }while(x<6);
x:=3
x:=4
x:=5
x:=6
undefined
>
Ctrl + C退出循环或者当前终端;
> a * b
30
> var c = _
undefined
> console.log(c)
30
下划线(_)变量
可以使用下划线(_)获取表达式的运算结果↑
二、事件循环
这个小demo差点成为我的瓶颈(想想也很可笑的,居然这样了,习惯太差)
//引入 events 模块
//var events = require('events');
////创建 eventEmitter 对象
////var eventEmitter = new events.EventEmitter();
var eventEmitter = new (require('events')).EventEmitter();
//////创建 eventEmitter 对象
//var eventEmitter = new events.EventEmitter();
////绑定事件及事件的处理程序
//eventEmitter.on('eventname',eventHandler);
////触发事件
//eventEmitter.emit('eventname');
//创建事件处理程序
var connectHandler = function connected(){
console.log('第①步--连接成功!');
//触发事件 data_received
eventEmitter.emit('data_received');
var x = 5,y = 3;
console.log("第③步--"+(x+y));
}
兰青色部分必须之前定义
//绑定 connection 事件处理程序
eventEmitter.on('connection',connectHandler);
//使用匿名函数绑定 data_received事件
eventEmitter.on('data_received',function(){
console.log("第②步--数据接收成功!");
})
//触发 connection 事件
eventEmitter.emit("connection");
console.log("第④步--events over!");
E:\node>node main.js
first--cuccess��
second--data success��
third--8
forth--events over!
E:\node>node main.js
第①步--连接成功!
第②步--数据接收成功!
第③步--8
第④步--events over!
var events = require("events"); //注意是events,注意s第二个错误
var emitter = new events.EventEmitter();
emitter.on('someEvent',function(arg1, arg2){
console.log('listener1', arg1, arg2);
});
emitter.on('someEvent', function(arg1, arg2){
console.log('listener2', arg1, arg2);
});
emitter.emit('someEvent', 'arg1 参数', 'arg2 参数');
E:\node\event.js:11 注意封号
emitter.emit('someEvent', 'arg1 鍙傛暟'锛?'arg2 鍙傛暟')锛?
^
SyntaxError: Unexpected token ILLEGAL
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
E:\node>node event.js
module.js:340
throw err;
^
Error: Cannot find module 'event'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (E:\node\event.js:2:13)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
E:\node>node event.js
listener1 arg1 参数 arg2 参数
listener2 arg1 参数 arg2 参数
E:\node>
nodejs系列(二)REPL交互解释 事件循环的更多相关文章
- 【转向Javascript系列】从setTimeout说事件循环模型
本文首发在alloyteam团队博客,链接地址http://www.alloyteam.com/2015/10/turning-to-javascript-series-from-settimeout ...
- 乱七八糟Nodejs系列二:线程模型
上一篇中说了这样一句话:Nodejs和浏览器javascript一样,都是单线程,所以和传统的不一样,这个后面有机会再说.挖了坑就得填,哎. 1.一个例子 来看一个例子,这个例子来自async jav ...
- NodeJs系列二:你好,世界
安装nodejs 什么是nodejs中的模块 hello,world
- 【nodejs原理&源码赏析(7)】【译】Node.js中的事件循环,定时器和process.nextTick
目录 Event Loop 是什么? Event Loop 基本解释 事件循环阶段概览 事件循环细节 timers pending callbacks poll阶段 check close callb ...
- 深入理解javascript中的事件循环event-loop
前面的话 本文将详细介绍javascript中的事件循环event-loop 线程 javascript是单线程的语言,也就是说,同一个时间只能做一件事.而这个单线程的特性,与它的用途有关,作为浏览器 ...
- nodejs-REPL/回调函数/事件循环
REPL 回调函数 事件循环 REPL----------------------------------------------------- Node.js REPL(Read Eval Prin ...
- 深入理解 JavaScript 事件循环(二)— task and microtask
引言 microtask 这一名词是 JS 中比较新的概念,几乎所有人都是在学习 ES6 的 Promise 时才接触这一新概念,我也不例外.当我刚开始学习 Promise 的时候,对其中回调函数的执 ...
- [译] 深入理解 JavaScript 事件循环(二)— task and microtask
引言 microtask 这一名词是 JS 中比较新的概念,几乎所有人都是在学习 ES6 的 Promise 时才接触这一新概念,我也不例外.当我刚开始学习 Promise 的时候,对其中回调函数的执 ...
- 【nodejs原理&源码赏析(7)】【译】Node.js中的事件循环,定时器和process.nextTick
[摘要] 官网博文翻译,nodejs中的定时器 示例代码托管在:http://www.github.com/dashnowords/blogs 原文地址:https://nodejs.org/en/d ...
随机推荐
- Java简单类——双向一对多映射
class Item { // 父栏目 private int iid ; private String name ; private String note ; private Subitem su ...
- php发送post请求到nodejs服务器
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data ); 改为 curl_setopt ( $ch, CURLOPT_POSTFIELDS, http_build ...
- 转:EntityFramework查询--联合查询(Join,GroupJoin)
首先我们先看一下Join public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this ...
- Solr6.2.0 + zookeeper 集群配置
zookeeper1 : 192.168.1.103zookeeper2 : 192.168.1.104zookeeper3 : 192.168.1.105solr1 : 192.168.1.106s ...
- 一种在视频OBJECT标签上放置均分四个区域的框选方法
一般在视频区域中做框样式,作应由视频插件自己来实现,但是出于其它一些原因自己琢磨了一个使用HTML标签来实现框选区域的方法,按照行外应该属于笨方法,虽然有点笨,可能在其他方面有借鉴意义,在这里拿出来跟 ...
- NSFileManager 遍历目录
NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *documentPath = [NSHomeDirecto ...
- Leetcode: All O`one Data Structure
Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...
- SGU 455 Sequence analysis(Cycle detection,floyd判圈算法)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=455 Due to the slow 'mod' and 'div' operati ...
- ASP.NET MVC URL重写与优化(1)-使用Global路由表定制URL
ASP.NET MVC URL重写与优化(1)-使用Global路由表定制URL 引言--- 在现今搜索引擎制霸天下的时代,我们不得不做一些东西来讨好爬虫,进而提示网站的排名来博得一个看得过去的流量. ...
- ESB 中的流量控制
ESB 中的流量控制