编程语言


nodejs 中模拟实现 emmiter 自定义事件

网络编程 nodejs 中模拟实现 emmiter 自定义事件 06-21

nodejs 中模拟实现 emmiter 自定义事件

<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title></title>
  <script>
   function Emitter() {
    this.events = {}; //存放事件的地方
   }
   Emitter.prototype.on = function(type, cb) {
    var events = this.events; 
    events = events[type] = events[type] || [];
    events.push(cb);
   };
   
   Emitter.prototype.emit = function(type) {
    var args = [].slice.call(arguments, 1);
    var cbs = this.events[type], cb;
    while (cb = cbs && cbs.shift()) {
     cb.apply(this, args);
    }
   };
   var emitter = new Emitter();
   emitter.on('customevent', function(param) {
    alert(param);
   });
   emitter.on('customevent', function() {
    alert(1);
   });
   emitter.emit('customevent', 'xxx');
  </script>
 </head>
 <body>
 </body>
</html>

你所未知的3种Node.js代码优化方式
Node.js程序的运行可能会受CPU或输入输出操作的限制而十分缓慢。从CPU角度看,程序运行缓慢的典型原因之一就是未经优化的「热点路径」(一段经常被

Node.js编写组件的三种实现方式
首先介绍使用v8API跟使用swig框架的不同:(1)v8API方式为官方提供的原生方法,功能强大而完善,缺点是需要熟悉v8API,编写起来比较麻烦,是js强相关

用NODE.JS中的流编写工具是要注意的事项
Node.js中的流十分强大,它对处理潜在的大文件提供了支持,也抽象了一些场景下的数据处理和传递。正因为它如此好用,所以在实战中我们常常基于它


编辑:编程语言

标签:方式,缓慢,事件,程序,热点