如何深入解析JavaScript事件模型及自定义事件实例?

2026-05-20 09:291阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计543个文字,预计阅读时间需要3分钟。

如何深入解析JavaScript事件模型及自定义事件实例?

JavaScript实现一个简单的事件模型,需要包含事件绑定与解绑功能。代码如下:

javascriptvar eventModel={ list: {}, bind: function () { var args=[...arguments]; var type=args[0]; var handlers=args.slice(1); if (typeof type==='string') { if (!this.list[type]) { this.list[type]=[]; } this.list[type].push(handlers); } }, unbind: function () { var args=[...arguments]; var type=args[0]; if (typeof type==='string') { if (this.list[type]) { this.list[type]=[]; } } }};

JavaScript 一个最简单的事件模型,需要有事件绑定与触发,还有事件删除。

var eventModel = { list: {}, bind: function () { var args = [].slice.call(arguments), type = args[0], handlers = args.slice(1); if (typeof type === 'string' && handlers.length > 0) { for (var i = 0; i < handlers.length; i++) { if (typeof handlers[i] === 'function') { if (!this.list[type]) { this.list[type] = []; } this.list[type].push(handlers[i]); } } } }, unbind: function () { var type = arguments[0], handlers = Array.prototype.slice.call(arguments, 1); if (typeof type === 'string') { if (handlers.length === 0) { this.list[type] = []; } else { for (var i = 0; i < handlers.length; i++) { if (typeof handlers[i] === 'function' && handlers[i] === this.list[type][i]) { this.list[type].splice(i, 1); } } } } }, trigger: function () { var arguments = [].slice.call(arguments), type = arguments[0], args = arguments[1] instanceof Array && !arguments[2] ? arguments[1] : arguments.slice(1), handlers = this.list[type]; for (var i = 0; i < handlers.length; i++) { handlers[i].apply(this, args.splice(0, handlers[i].length)); } } };

其中主要实现了bind(绑定事件)、unbind(删除事件)与 trigger (触发事件)。对同一事件名称,可以绑定多个事件处理函数;并按照绑定的顺序依次触发。

args.splice(0, handlers[i].length) 触发时的传参

事件绑定与触发:

如何深入解析JavaScript事件模型及自定义事件实例?

eventModel.bind('myevent1', function (a) { console.log(a); // 1 }, function(b) { console.log(b); // 2 }, function(c, d) { console.log(c + ' + ' + d); // a + b }); eventModel.bind('myevent1', function (e) { console.log(e); // 50 }); eventModel.trigger('myevent1', 1,2,'a','b', 50);

事件删除:

<button id="bind">bind</button> <button id="unbind">unbind</button>

var fnX = function () { console.log('fnX'); } var fnY = function () { console.log('fnY'); } eventModel.bind('myevent2', fnX, fnY); document.getElementById('unbind').onclick = function () { eventModel.unbind('myevent2', fnX); //删除 fnX 后,只剩下 fnY }; document.getElementById('bind').onclick = function () { eventModel.trigger('myevent2'); //输出 fnX fnY //在点击unbind后,只输出 fnY };

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持易盾网络!

本文共计543个文字,预计阅读时间需要3分钟。

如何深入解析JavaScript事件模型及自定义事件实例?

JavaScript实现一个简单的事件模型,需要包含事件绑定与解绑功能。代码如下:

javascriptvar eventModel={ list: {}, bind: function () { var args=[...arguments]; var type=args[0]; var handlers=args.slice(1); if (typeof type==='string') { if (!this.list[type]) { this.list[type]=[]; } this.list[type].push(handlers); } }, unbind: function () { var args=[...arguments]; var type=args[0]; if (typeof type==='string') { if (this.list[type]) { this.list[type]=[]; } } }};

JavaScript 一个最简单的事件模型,需要有事件绑定与触发,还有事件删除。

var eventModel = { list: {}, bind: function () { var args = [].slice.call(arguments), type = args[0], handlers = args.slice(1); if (typeof type === 'string' && handlers.length > 0) { for (var i = 0; i < handlers.length; i++) { if (typeof handlers[i] === 'function') { if (!this.list[type]) { this.list[type] = []; } this.list[type].push(handlers[i]); } } } }, unbind: function () { var type = arguments[0], handlers = Array.prototype.slice.call(arguments, 1); if (typeof type === 'string') { if (handlers.length === 0) { this.list[type] = []; } else { for (var i = 0; i < handlers.length; i++) { if (typeof handlers[i] === 'function' && handlers[i] === this.list[type][i]) { this.list[type].splice(i, 1); } } } } }, trigger: function () { var arguments = [].slice.call(arguments), type = arguments[0], args = arguments[1] instanceof Array && !arguments[2] ? arguments[1] : arguments.slice(1), handlers = this.list[type]; for (var i = 0; i < handlers.length; i++) { handlers[i].apply(this, args.splice(0, handlers[i].length)); } } };

其中主要实现了bind(绑定事件)、unbind(删除事件)与 trigger (触发事件)。对同一事件名称,可以绑定多个事件处理函数;并按照绑定的顺序依次触发。

args.splice(0, handlers[i].length) 触发时的传参

事件绑定与触发:

如何深入解析JavaScript事件模型及自定义事件实例?

eventModel.bind('myevent1', function (a) { console.log(a); // 1 }, function(b) { console.log(b); // 2 }, function(c, d) { console.log(c + ' + ' + d); // a + b }); eventModel.bind('myevent1', function (e) { console.log(e); // 50 }); eventModel.trigger('myevent1', 1,2,'a','b', 50);

事件删除:

<button id="bind">bind</button> <button id="unbind">unbind</button>

var fnX = function () { console.log('fnX'); } var fnY = function () { console.log('fnY'); } eventModel.bind('myevent2', fnX, fnY); document.getElementById('unbind').onclick = function () { eventModel.unbind('myevent2', fnX); //删除 fnX 后,只剩下 fnY }; document.getElementById('bind').onclick = function () { eventModel.trigger('myevent2'); //输出 fnX fnY //在点击unbind后,只输出 fnY };

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持易盾网络!