【JavaScript】Registering JavaScript object methods as callbacks
The registration of callback functions is very common in JavaScript web programming, for example to attach user interface event handlers (such as onclick), or to provide a function to handle an XHR response. Registering an object method as a callback function is not entirely straightforward, but there are a number of approaches that we can use.
Let’s say we’ve got a constructor, an object, and a function that registers a callback:
function MyObject(val){
this.val = val;
}
MyObject.prototype.alertVal = function(){
alert(this.val);
}
var obj = new MyObject(8);
function register(callback){
// some time later...
callback();
}
The constructor stores its single argument, and the alertVal() method alerts it. The simple register() function takes a function and calls it. In a real situation the behaviour here would be much more interesting but this will do for illustration.
Why we don’t want to just pass obj.alertVal to register()
Object methods are first-class functions in JavaScript and we could pass obj.alertVal to register() – but it isn’t quite what we want. Let’s see what happens:
register(obj.alertVal);
// inside register(), callback === obj.alertVal
callback()
// inside MyObject.prototype.alertVal
alert(this.val);
// because callback() was not called on an object,
// this === the JavaScript global object and not obj;
// this.val is the global variable val, not obj.val
When a function is called as a method on an object (obj.alertVal()), "this" is bound to the object that it is called on (obj). And when a function is called without an object (func()), "this" is bound to the JavaScript global object (window in web browsers.) When we passed obj.alertVal to register() we were passing a reference to the function bound to obj.alertVal, but no reference to the object obj.
So, we need to bind our method to the object.
Closure with an anonymous function
In JavaScript, whenever a function is defined within another one a closure is created [JavaScript Closures for Dummies] [JavaScript Closures]. A closure remembers the variable bindings that were in scope when the function was created. These bindings are then available whenever the function is called. We can bind our method to our object instance with the following:
register(function(){obj.alertVal()});
Whenever the anonymous function is called, "obj" will be bound to the value that it had when the function was created. Which is exactly what we want.
(If we execute the above code outside a function it will behave differently. No closure will be created, instead, the current value of the global variable "obj" will be used whenever the anonymous function is called, not the value at function definition.)
If we want to register a method on the object currently bound to "this", we need to take an extra step:
var obj = this;
register(function(){obj.alertVal()});
If we don’t explicitly bind "this" to a named variable (obj) and instead use register(function(){this.alertVal()}) we will lose our object reference. "this" will be bound to the JavaScript global object whenever the anonymous function is called.
Build a generic closure maker
Instead of building a closure each time we want to register a method as a callback, we could write a utility function to do it for us. For example:
function bind(toObject, methodName){
return function(){toObject[methodName]()}
}
With such a function we can then register our method with:
register(bind(obj, "alertVal"));
Dojo (dojo.hitch()) and Prototype (bind()) both have such utility functions (that also allow you to provide arguments to pass to the method when called, something that our "bind" doesn’t do.)
If we want to register a method of "this", we don’t need to explicitly bind it (as we did above) before calling "bind" – the function call does the binding for us. register(bind(this, "alertVal"))works as expected.
Alter the register function to take the object too
If we changed our register function to:
function register(anObject, methodName){
// some time later...
anObject[methodName]();
}
We could register our call with:
register(obj, "alertVal");
dojo.connect and YUI’s YAHOO.util.Event.addListener() [YUI Event Utility] [YAHOO.util.Event API docs] both include this binding style in their API.
Bind the method to the object at construction
We could bind our method to the object (or instance variables as shown here) in the constructor function:
function MyObject(val){
this.alertVal = function(){
alert(val);
}
}
We could then register obj.alertVal directly as "val" is already bound:
obj = new MyObject(8);
register(obj.alertVal);
Douglas Crockford writes about this programming style in Private Members in JavaScript.
Circular references and memory leaks
Whichever method you use you need to be careful about avoiding circular references when registering event handlers (such as onclick) on document objects. For example, if we register an object method as an event handler on an element, such that the method is bound to the object, and the object has a reference back to the element, then we have a circular reference, and a potential leak (in this case a solution would be to have the object store the element’s id rather than store a reference to the element object itself.) Here are some articles that discuss ways to cause memory leaks and ways to avoid them:
- Memory leak patterns in JavaScript by Abhijeet Bhattacharya and Kiran Shivarama Sundar (IBM developerWorks)
- Understanding and Solving Internet Explorer Leak Patterns by Justin Rogers (Microsoft)
- JavaScript Closures by Richard Cornford
http://www.bitstructures.com/2007/11/javascript-method-callbacks.html
【JavaScript】Registering JavaScript object methods as callbacks的更多相关文章
- 【译】延迟加载JavaScript
[译]延迟加载JavaScript 看到一个微信面试题引发的血案 --[译] 什么阻塞了 DOM?中提到的一篇文章,于是决定看下其博客内容,同时翻译下来留作笔记,因英文有限,如有不足之处,欢迎指出.同 ...
- 【概率论】1-3:组合(Combinatorial Methods)
title: [概率论]1-3:组合(Combinatorial Methods) categories: Mathematic Probability keywords: Combination 组 ...
- 【WIP】客户端JavaScript Web Object
创建: 2017/10/11 更新: 2017/10/14 标题加上[WIP],增加[TODO] 更新: 2018/01/22 更改标题 [客户端JavaScript Web Object, UR ...
- Python开发【前端】:JavaScript
JavaScript入门 JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本 ...
- 【拾遗】理解Javascript中的Arguments
前言 最近在看JavaScript相关的知识点,看到了老外的一本Javascript For Web Developers,遇到了一个知识盲点,觉得老外写的很明白很透彻,记录下来加深印象,下面是我摘出 ...
- 【WIP】客户端JavaScript 事件处理
创建: 2017/10/15 完成: 2017/10/15 更新: 2017/11/04 加粗事件的参数 更新: 2017/12/12 增加事件处理时获取事件对象的方法 更新: 2019/05/2 ...
- 【JS】312- 复习 JavaScript 严格模式(Strict Mode)
点击上方"前端自习课"关注,学习起来~ 注:本文为 < JavaScript 完全手册(2018版) >第30节,你可以查看该手册的完整目录. 严格模式是一项 ES5 ...
- 【repost】图解Javascript上下文与作用域
本文尝试阐述Javascript中的上下文与作用域背后的机制,主要涉及到执行上下文(execution context).作用域链(scope chain).闭包(closure).this等概念. ...
- 【WIP】客户端JavaScript DOM
创建: 2017/10/12 初步完成: 2017/10/15 更新: 2017/10/14 标题加上[WIP],继续完成 [TODO] 补充暂略的, 搜[略] DOM树 概要 基本 ...
随机推荐
- 安卓动画之ObjectAnimator
ObjectAnimator 不仅仅移动位置,还移动了对象view 先来代码片段: //Y轴变换 ObjectAnimator oa = ObjectAnimator.ofFloat(imageVie ...
- 遵守GPL的开源软件能用于商用吗?
遵守GPL的开源软件能用于商用吗? 比较经典的开源协议有 GPL,BSD 等等. GPL 软件可以用于商业用途,甚至说,RMS 撰写 GPL 协议的目的就是为了让自己的 GPL 软件 emacs 可以 ...
- vmware10中开启Intel VT-x
记得刚接触linux的时候,是在win7下使用vmware虚拟机来安装linux,这样就可以方便的一边使用win7娱乐,一边在linux下进行学习.后来发现这种方式使得win7很卡,虚拟机也很卡,让人 ...
- 关于对javascript 提升概念 的总结与思考。
最近在看一本新买的书叫<你不知道的javascript上卷>..买到这本书也算是个巧合 不过真是物有所值.它对js的几个高级概念都做了非常深刻的描述和通过一些通俗易懂的方式来让我们理解.这 ...
- MVC如何配置才能访问静态页面
默认在Views文件外的静态页面可以访问,若要访问Views里的静态页面则需要修改View文件夹中的web.config: <system.webServer> <handlers& ...
- C语言中返回字符串函数的四种实现方法 2015-05-17 15:00 23人阅读 评论(0) 收藏
C语言中返回字符串函数的四种实现方法 分类: UNIX/LINUX C/C++ 2010-12-29 02:54 11954人阅读 评论(1) 收藏 举报 语言func存储 有四种方式: 1.使用堆空 ...
- C++11变长参数模板
[C++11变长参数模板] C++03只有固定模板参数.C++11 加入新的表示法,允许任意个数.任意类别的模板参数,不必在定义时将参数的个数固定. 实参的个数也可以是 0,所以 tuple<& ...
- manacher算法(转载)
原网址:http://blog.sina.com.cn/s/blog_70811e1a01014esn.html manacher算法是我在网上无意中找到的,主要是用来求某个字符串的最长回文子串.不过 ...
- How Tomcat Works(十五)
本文接下来分析Context容器,Context容器实例表示一个具体的Web应用程序,其中包括一个或多个Wrapper实例:不过Context容器还需要其他的组件支持,典型的如载入器和Session管 ...
- thinkphp过滤html、script
使用tp3.1版本 1.APP/common 自定义函数 function filter_default(&$value){ $value = htmlspecialchars($value) ...