node.js 中events事件【重要】

node 专栏收录该内容
23 篇文章 0 订阅

夜深了,人静了,灵感来了,敲敲代码。 

1、用法


// 非阻塞I/O
const EventEmitter=require('events');   //可以直接引用的模块,不需要自己写,或者引包,或者引入模块,就是核心模块

// 核心就是发布订阅  on 订阅   emit 发布  off 取消订阅   绑定一次  once
const events=new EventEmitter;


const cry=function(name){
    console.log('哭',name);
}

events.on('女孩失恋了',cry);

events.on('女孩失恋了',function(name){
    console.log('笑',name);
});

// 只执行,执行完毕后就将该函数移除,下次不会在继续触发了。

events.once('女孩失恋了',function(name){
    console.log('逛街',name);
});


events.off('女孩失恋了',cry);

events.emit('女孩失恋了','boy');
events.emit('女孩失恋了','boy');
/* process.nextTick(()=>{
    // nextTick  可以解决异步调用问题,方式优于promise.then();
    events.emit('女孩失恋了','boy');
}) */


// 先订阅,再发布

// vue3  mitt


// 一般的继承:实例继承+原型继承+类属性继承 = es6 的extend

// 原型继承有3种方法

function Girl(){

}
/* Object.setPrototypeOf(Girl.prototype,EventEmitter.prototype);   //es6
Girl.prototype=Object.create(EventEmitter);   // es5 
Girl.prototype._proto_=EventEmitter.prototype;  // es */  // 多

// node.js 中提供的方法 原型继承
const util=require('util');
util.inherits(Girl,EventEmitter);


// splice 和filter 删除

2、实现原理


// node 内部实现的还是构造函数

function EventEmitter(){
    this._events={};
}

EventEmitter.prototype.on=function(eventName,callback){
    if(!this._events) this._events={};
    const callbacks=this._events[eventName] ||[];
    callbacks.push(callback);
    this._events[eventName]=callbacks;

    console.log(this._events);
}

EventEmitter.prototype.emit=function(eventName,...args){
    if(!this._events) this._events={};
    let callbacks=this._events[eventName];
    if(callbacks){
        callbacks.forEach(cb => {
            cb(...args);
        });
    }
}

EventEmitter.prototype.off=function(eventName,callback){
    if(!this._events) this._events={};
    if(this._events[eventName]){
        this._events[eventName]=this._events[eventName].filter(fn=>fn!=callback && fn.l !=callback)
    }
    
}

EventEmitter.prototype.once=function(eventName,callback){
    // 先绑定,执行完成后再删除
    const one=(...args)=>{
        callback(...args);

        // 执行完毕后
        // 删除事件

        this.off(eventName,one);
    }

    // 先绑定事件

    one.l=callback;   // 自定义属性  创建关联

    this.on(eventName,one);
}

module.exports=EventEmitter;


// 或  第一个为真,就不走后面了。

// on  emit once  off 

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
Since it’s creation in 2009, Node.js has grown into a powerful and increasingly popular asynchronous-development framework for creating highly-scalable network applications using JavaScript. Respected companies such as Dow Jones and LinkedIn are among the many organizations to have seen Node’s potential and adopted it into their businesses. Pro Node.js for Developers provides a comprehensive guide to this exciting new technology. We introduce you to Node – what it is, why it matters and how to set it up – before diving deeply into the key concepts and APIs that underpin its operation. Building upon your existing JavaScript skills you’ll be shown how to use Node.js to build both Web- and Network-based applications, to deal with data sources, capture events and deal with child processes to create robust applications that will work well in a wide range of circumstances. Once you’ve mastered these skills we’ll go further, teaching you more advanced software engineering skills that will give your code a professional edge. You’ll learn how to create easily reusable modules that will save you time through code reuse, to log and debug your applications quickly and effectively and to write code that will scale easily and reliably as the demand for your application grows. What you’ll learn Install, configure and deploy Node.js apps effectively Understand the Node.js asynchronous programming model in detail Create both web and network-based Node.js applications with ease Learn to work effectively with varied data sources and file types Discover advanced software engineering concepts that will will save you time and promote code reuse Who this book is for This book is for developers who already have a working knowledge of JavaScript and are interested in adopting Node.js into their projects. All other concepts will be introduced from first-principals. Table of Contents Chapter 1. Getting Started Chapter 2. The Node Module System Chapter 3. The Node Programming Model Chapter 4. Events and Timers Chapter 5. The Command Line Interface Chapter 6. The File System Chapter 7. Streams Chapter 8. Binary Data Chapter 9. Executing Code Chapter 10. Network Programming Chapter 11. HTTP Chapter 12. The Express Framework Chapter 13. The Real-Time Web Chapter 14. Databases Chapter 15. Logging, Debugging, and Testing Chapter 16. Application Scaling Appendix A. JavaScript Object Notation
©️2020 CSDN 皮肤主题: 终极编程指南 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值