理解 backbone.js 中的 bind 和 bindAll 方法,关于如何在方法中指定其中的 this,包含apply方法的说明[转载]
转载自:http://gxxsite.com/content/view/id/132.html
在backbone.js的学习过程中,被bind和bindAll弄得有点晕,这里包括underscore.js的bind和bindAll,以及JQuery提供的bind方法。
在一篇En博客中学习,写下这篇笔记
1、首先说熟悉的JQuery的bind,引用api帮助文件的内容即可很清晰地理解其使用意义和方法:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
dom.bind(type,[data],function(eventObject));dom.bind(type,[data],false);dom.bind(events);//例子//当每个段落被点击的时候,弹出其文本:$("p").bind("click", function(){ alert( $(this).text() );});//同时绑定多个事件类型:$('#foo').bind('mouseenter mouseleave', function() { $(this).toggleClass('entered');});//同时绑定多个事件类型/处理程序:$("button").bind({ click:function(){$("p").slideToggle();}, mouseover:function(){$("body").css("background-color","red");}, mouseout:function(){$("body").css("background-color","#FFFFFF");} });//你可以在事件处理之前传递一些附加的数据:function handler(event) { alert(event.data.foo);}$("p").bind("click", {foo: "bar"}, handler)//通过返回false来取消默认的行为并阻止事件起泡:$("form").bind("submit", function() { return false; })//通过使用 preventDefault() 方法只取消默认的行为:$("form").bind("submit", function(event){ event.preventDefault();});//通过使用 stopPropagation() 方法只阻止一个事件起泡:$("form").bind("submit", function(event){ event.stopPropagation();}); |
2、underscore.js的apply方法
apply主要作用是让我们可以控制方法中this指代的值,下面用代码表述:
|
1
2
3
4
5
|
var func = function beautiful(){ alert(this + ' is beautiful');};func.apply('Internet');//输出Internet is beautiful |
以上例子只帮我们理解apply的作用,实际上,apply的意义何在,请看下例:
|
1
2
3
4
5
6
7
8
9
10
|
function Developer(skill) { this.skill = skill; this.says = function(){ alert(this.skill + ' rocks!'); }}var john = new Developer('Ruby');var func = john.says;func();//输出undefined rocks! |
上例可看出,在给调用对象john中的says方法定义一个单独的方法func后,执行func,this将被认为是func所处的对象,而不是john。这时apply可以解决问题,代码如下:
|
1
2
3
4
5
6
7
8
9
10
|
function Developer(skill) { this.skill = skill; this.says = function(){ alert(this.skill + ' rocks!'); }}var john = new Developer('Ruby');var func = john.says;func.apply(john);//输出Ruby rocks! |
这样做太复杂,所以需要用bind和bindAll来简化和规范化,请往下看。
3、underscore.js的bind方法
|
1
2
3
4
5
6
7
8
9
10
|
function Developer(skill) { this.skill = skill; this.says = function(){ alert(this.skill + ' rocks!'); }}var john = new Developer('Ruby');var func = _.bind(john.says, john); //绑定的方法是john对象执行says方法,里面的this指代的是第二个参数johnfunc();//输出Ruby rocks! |
注意:_.bind()返回的值才是绑定的方法,而不会影响里面绑定的方法本身,看下例:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
window.ProductView = Backbone.View.extrend({ initialize: function() { _.bind(this.render, this); this.model.bind('change', this.render); }});//这样做的结果是change触发的是原this.render,方法中的this依然是不可性预计window.ProductView = Backbone.View.extrend({ initialize: function() { var f_render=_.bind(this.render, this); this.model.bind('change', f_render); }});//这是正确做法,或者更直接简单:window.ProductView = Backbone.View.extrend({ initialize: function() { this.model.bind('change', _.bind(this.render, this)); }});//最简单当然是用_.bindAll:window.ProductView = Backbone.View.extrend({ initialize: function() { _.bindAll(this, this.render); this.model.bind('change', this.render); }}); |
4、underscore.js的bindAll方法
|
1
2
3
4
5
6
7
8
9
10
11
12
|
function Developer(skill) { this.skill = skill; this.says = function(){ alert(this.skill + ' rocks!'); }}var john = new Developer('Ruby');_.bindAll(john, 'says'); //绑定的方法是john中的says方法,里面的this指代john //可以一次过指定this到多个方法:_.bindAll(john,'says','work','gohome');var func = john.says;func();//输出Ruby rocks! |
参考:http://blog.bigbinary.com/2011/08/18/understanding-bind-and-bindall-in-backbone.html
理解 backbone.js 中的 bind 和 bindAll 方法,关于如何在方法中指定其中的 this,包含apply方法的说明[转载]的更多相关文章
- Backbone.js的技巧和模式
Backbone.js的技巧和模式 Backbone.js的技巧和模式 本文由白牙根据Phillip Whisenhunt的<Backbone.js Tips And Patterns> ...
- Js apply方法与call方法详解 附ES6新写法
我在一开始看到javascript的函数apply和call时,非常的模糊,看也看不懂,最近在网上看到一些文章对apply方法和call的一些示例,总算是看的有点眉目了,在这里我做如下笔记,希望和大家 ...
- Js apply 方法 具体解释
Js apply方法具体解释 我在一開始看到javascript的函数apply和call时,很的模糊,看也看不懂,近期在网上看到一些文章对apply方法和call的一些演示样例,总算是看的有点眉目了 ...
- Backbone中bind和bindAll的作用
本文参考: http://blog.bigbinary.com/2011/08/18/understanding-bind-and-bindall-in-backbone.html bindAll内部 ...
- 全面解析JavaScript的Backbone.js框架中的Router路由
这篇文章主要介绍了Backbone.js框架中的Router路由功能,Router在Backbone中相当于一个MVC框架中的Controller控制器功能,需要的朋友可以参考下. Backbone ...
- js中的bind方法的实现方法
js中目前我遇见的改变作用域的5中方法:call, apply, eval, with, bind. var obj = { color: 'green' } function demo () { c ...
- js中的bind、apply、call、callee、caller的区别
1.bind.apply与call的区别与使用 相同点:2者是函数原型的一个方法,因此调用者都必须是函数,第1个参数都是对象.作用是,用另一个对象替换当前对象,另一对象也即是你传的第一个参数.通常用于 ...
- js中bind,call,apply方法的应用
最近用js的类写东西,发现一个无比蛋疼的事,那就是封装的类方法中的this指针经常会改变指向,失去上下文,导致程序错误或崩溃. 比如: function Obj(){ this.type = &quo ...
- Backbone.js中的where和findWhere
小编的公司框架用的MVC框架依旧是Backbone.js,老大说框架不重要,重要的是框架的编程思想.于是乎,小编从头开始学习Backbone.走马观花似的看了下API文档,撸起袖子就是干.但是碰到一个 ...
随机推荐
- hdu 1732 Push Box
http://acm.hdu.edu.cn/showproblem.php?pid=1732 推箱子和游戏规则一样. #include <cstdio> #include <cstr ...
- BZOJ 2661 连连看
http://www.lydsy.com/JudgeOnline/problem.php?id=2661 思路:预处理出每个数字,然后若有x^2=y^2+z^2且z与y互质, s->x 1 ,0 ...
- Find out C++ Memory Usage in Code
You can use Microsoft Platform SDK functions to get the heap size at a specific point. If get the he ...
- WPF用样式实现TextBox的虚拟提示效果
[版权声明]本文为博主原创,未经允许禁止用作商业用途,如有转载请注明出处. 话说好多软件和网站都能实现虚拟提示,好吧这个名词是我自己起的,因为我也不知道这么形容这个效果. 效果描述:在TextBox没 ...
- mysql5.5 无法创建实例,error 16001
今天想用jdbc做个小程序,结果发现好久不用的mysql不好用了,我装的是社区版(win7)环境下,按理说不可能出问题,找了一堆解决方案都没解决,准备重装的时候想把mysql服务停了,直接在dos输入 ...
- 需要考虑的9个SEO实践
搜索引擎优化重要吗?我们知道,网站设计是把屏幕上平淡无奇变成令人愉快的美感,更直观地辨认信息.这也是人与人之间在沟通想法,这样的方式一直在演变. 穴居人拥有洞穴壁画,古埃及人有象形文字,现代人有网页设 ...
- Encode and Decode Strings 解答
Question Design an algorithm to encode a list of strings to a string. The encoded string is then sen ...
- IDX爱定客 | 氪加
IDX爱定客 | 氪加 个性化定制鞋网站,在线定制只需三分钟
- 【shell】构造并遍历二位数组的一种用法
参考shell数组的部分操作用法,实现了构造和遍历二维数组的一种方式,具体如下: #数组元素以空格分割 sites=("www.a.com www.b.com www.c.com www.d ...
- Linux 通过HTTP进行域名更新
一.3322动态域名更新接口 接口地址 API URL http://members.3322.net/dyndns/update HTTP请求 GET /dyndns/update?hostname ...