The Trick
The problem is as follows: Methods use this
to refer to the object it is a method of. But, when using a method as event handler or callback function, this
does no longer point to that object. The trick is to use the closure like behaviour of functions, so that the method will always have access to it's object.
Let's look at an example: We want to use a method of an Alerter class as an event handler.
function Alerter(text) {
this.text=text;
var me=this;
this.invoke=function () {
alert(me.text);
}
}
var sayHi=new Alerter('Hello, world!');
div.attachEvent('onclick', sayHi.invoke);
So, instead of using this
, we use a variable me
, that equals this
when the invoke
method is created. And, in contrast to this
, me
will keep refering to the correct Alerter
object, even when it's passed as a function to the attachEvent method of an HTML element.
Higher Order Programming again
window.setTimeout
is an often used function for dynamic webpages. It waits for a given amount of time, and then calls a callback function. The above defined sayHi
function can be used as a callback function: setTimeout(sayHi.invoke,2000)
will show an alert box after 2 seconds. But what if we want to be extra cool and show 2 alert boxes after those 2 seconds? Then we'd have to create a new function that calls both Alerter objects:
var sayHi=new Alerter('Hello, world!');
var sayBye=new Alerter('Bye, world!');
setTimeout(function() {sayHi.invoke();sayBye.invoke();},2000);
A nice feature of Microsoft's delegates is that you can create a single composite deligate from several delegates. It looks less messy than inserting an anonymous function. Let's do that in Javascript too, by extending the function prototype.
Function.prototype.andThen=function(g) {
var f=this;
return function() {
f();g();
}
};
Now we can write:
setTimeout((sayHi.invoke).andThen(sayBye.invoke),2000);
Several callbacks
With the andThen
method, it becomes very easy to create an object that allows several other objects to register callbacks.
function Manager() {
this.callback=function () {}; // do nothing
this.registerCallback=function(callbackFunction) {
this.callback=(this.callback).andThen(callbackFunction);
}
}
var manager=new Manager();
manager.registerCallback(sayHi.invoke);
manager.registerCallback(sayBye.invoke);
manager.callback();
分享到:
相关推荐
官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,亲测可用
python库。 资源全名:pydap.handlers.netcdf-0.6.1-py2.6.egg
《Python库lambda_handlers-1.0.3-py3-none-any.whl详解》 在Python的开发环境中,库是开发者的重要工具,它们提供了丰富的功能,简化了编程过程。本文将详细探讨一个名为“lambda_handlers”的Python库,以及其在...
《PyPI上的Python数据库处理库pydap.handlers.sql-0.1.1-py2.7.egg详解》 PyPI(Python Package Index)是Python开发者广泛使用的软件包仓库,其中包含了丰富的Python库和模块。在PyPI官网上,我们可以找到各种各样...
资源来自pypi官网。 资源全名:lambda_handlers-1.0.3-py3-none-any.whl
python库。 资源全名:fabric_am_handlers-0.12-py3-none-any.whl
│ └── handlers │ └── users.js │ └── lib ├── package.json └── serverless.yml 组态 打开serverless.yml配置文件,然后: 添加plugins部分 在其中添加我们新的serverless-func
【标题】"Developer's Guide - Event Handlers" 是一篇关于开发者如何在编程中处理事件的指南。这个主题在软件开发,尤其是交互式应用开发中至关重要,因为它涉及到用户与应用程序的交互方式。事件处理器是程序响应...
资源分类:Python库 所属语言:Python 资源全名:django-ohm2-handlers-light-0.1.19.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源分类:Python库 所属语言:Python 资源全名:pollination_handlers-0.5.0-py2.py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
总结了IE 5 ,页面的所有事件 Dom Event Handlers/Dom 事件处理函数
官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装
JavaScript事件处理器是JavaScript编程中的核心概念,用于处理用户与网页之间的交互。...理解并熟练掌握JavaScript事件处理机制对于创建动态和交互性强的网页至关重要。 事件是浏览器或JavaScript引擎在特定时刻触发的...
1. **事件处理程序(Event Handlers)** - 事件处理程序是JavaScript中处理事件的函数。你可以将它们绑定到HTML元素,当特定事件发生时,这些函数会被调用。 - 有多种方式设置事件处理程序:`addEventListener`、`...
官方版本,亲测可用