一、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交互解释 事件循环的更多相关文章

  1. 【转向Javascript系列】从setTimeout说事件循环模型

    本文首发在alloyteam团队博客,链接地址http://www.alloyteam.com/2015/10/turning-to-javascript-series-from-settimeout ...

  2. 乱七八糟Nodejs系列二:线程模型

    上一篇中说了这样一句话:Nodejs和浏览器javascript一样,都是单线程,所以和传统的不一样,这个后面有机会再说.挖了坑就得填,哎. 1.一个例子 来看一个例子,这个例子来自async jav ...

  3. NodeJs系列二:你好,世界

    安装nodejs 什么是nodejs中的模块 hello,world

  4. 【nodejs原理&源码赏析(7)】【译】Node.js中的事件循环,定时器和process.nextTick

    目录 Event Loop 是什么? Event Loop 基本解释 事件循环阶段概览 事件循环细节 timers pending callbacks poll阶段 check close callb ...

  5. 深入理解javascript中的事件循环event-loop

    前面的话 本文将详细介绍javascript中的事件循环event-loop 线程 javascript是单线程的语言,也就是说,同一个时间只能做一件事.而这个单线程的特性,与它的用途有关,作为浏览器 ...

  6. nodejs-REPL/回调函数/事件循环

    REPL 回调函数 事件循环 REPL----------------------------------------------------- Node.js REPL(Read Eval Prin ...

  7. 深入理解 JavaScript 事件循环(二)— task and microtask

    引言 microtask 这一名词是 JS 中比较新的概念,几乎所有人都是在学习 ES6 的 Promise 时才接触这一新概念,我也不例外.当我刚开始学习 Promise 的时候,对其中回调函数的执 ...

  8. [译] 深入理解 JavaScript 事件循环(二)— task and microtask

    引言 microtask 这一名词是 JS 中比较新的概念,几乎所有人都是在学习 ES6 的 Promise 时才接触这一新概念,我也不例外.当我刚开始学习 Promise 的时候,对其中回调函数的执 ...

  9. 【nodejs原理&源码赏析(7)】【译】Node.js中的事件循环,定时器和process.nextTick

    [摘要] 官网博文翻译,nodejs中的定时器 示例代码托管在:http://www.github.com/dashnowords/blogs 原文地址:https://nodejs.org/en/d ...

随机推荐

  1. AFNetworking 提示"The resource could not be loaded because the App Transport Security policy requires the use of a secure connection" 解决办法

    原因:iOS9以后,苹果把原http协议改成了https协议,所以不能直接在http协议下GET/POST 解决方案之一: 直接编辑工程文件下的Info.plist文件,加入以下代码 <key& ...

  2. Wysiwyg Editors 标签过滤

    针对October CMS编辑器插件取消自动过滤DIV标签开关: 找到modules\backend\formwidgets\richeditor\assets\vendor\redactor\red ...

  3. linux 查找命令

    查找目录:find /(查找范围) -name '查找关键字' -type d查找文件:find /(查找范围) -name 查找关键字 -print

  4. wireless tool 移植

    在linux上调试wifi, 需要移植wireless tool进行验证,本文记录移植方法. 参考链接 http://www.cnblogs.com/zengjfgit/p/5601473.html ...

  5. Pivot C# WPF 代码添加PivotItem

    PivotItem new_PivotItem = new PivotItem();//新建Pivotitem ListView new_ListView = new ListView();//新建l ...

  6. Dedecms织梦系统修改文章描述字数长度的方法

    1进入数据库修改表: dede_archives, 字段 description  varchar(500): 2 进入织梦后台,找到 系统-->系统基本参数--> 其它选项自动摘要长度( ...

  7. cat <<EOF用法

    转自:http://blog.csdn.net/apache0554/article/details/45508631 cat <<EOF和cat <<-EOF两个都是获取st ...

  8. SVD奇异值分解

    奇异值分解 备忘:Eigen类库可能会和其他库产生冲突,将Eigen类库的头文件引用放到前面解决了.

  9. TCP/IP网络编程中socket的行为

    一. read/write的语义:为什么会阻塞? 先从write说起: #include <unistd.h>ssize_t write(int fd, const void *buf, ...

  10. Force StyleCop to Ignore a File

    You can quickly force StyleCop to ignore files in a project by manually modifying the project file, ...