一个简单的前端事件框架
2018-02-01 15:00
309 查看
参考网上的一个资料,做下备注。
<html> <head> <title>js event demo</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0" max-age="0"> </head> <body> <h4>js event demo</h4> </body> <script type="text/javascript"> //自定义事件 function EventEmitter() { this.events = {}; } //绑定事件函数 EventEmitter.prototype.on = function(ename, call){ this.events[ename] = this.events[ename] || []; this.events[ename].push(call); } EventEmitter.prototype.emit = function(ename, _){ var events = this.events[ename]; //取参数,剔除参数ename var args = Array.prototype.slice.call(arguments, 1); for(var i = 0; i < events.length; i++){ //调用绑定的事件函数 events[i].apply(null, args); } } function app(){ calltime = 0; //同一个事件绑定了两个处理函数 this.on('start',function(user, date){ calltime += 1; console.log('event start: ' + user + " " + date + " " + calltime); }); this.on('start', function(user, date){ calltime += 1; console.log('event start: ' + user + " " + date + " " + calltime); }) } app.prototype = new EventEmitter(); var a = new app(); //触发事件 a.emit('start', 'fred', new Date()); </script> </html>
相关文章推荐
- 自己动手写一个简单的IOC框架,使用注解绑定资源和事件
- 搭建一个简单的cms前端框架
- leaflet一个前端gis框架,比openlayer更简单
- 一个简单的游戏开发框架(三.事件管理)
- JS 前端框架7 一个简单的require功能
- 结合nodejs,grunt,seajs及实际产品需要,设计一个简单的前端自动化构建框架
- Ajax一个好用简单框架Ajax.dll_________推荐
- 关于最近流行的vuejs.angularjs,Reactjs等前端框架的简单对比及体验
- 如何实现一个简单的MVVM框架
- 简单分享一个轻量级自动化测试框架目录结构设计
- 一个看上去简单的问题(1):Asp.Net动态设定事件;
- 一个简单的ssm框架的搭建
- ExtJS前端框架EXT弹出窗口事件
- 给对象增加一个简单的自定义事件机制
- dva一个react前端应用框架
- 一个简单的游戏服务器框架
- 简单的Java Event-事件框架
- [导入]ASP.NET MVC框架开发系列课程(2):一个简单的ASP.NET MVC应用程序.zip(13.70 MB)
- 一个php简单的框架实现,仅实现了简单路由层
- 一个简单的游戏服务器框架