理解 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文档,撸起袖子就是干.但是碰到一个 ...
随机推荐
- [POJ] 2352 Stars [线段树区间求和]
Stars Description Astronomers often examine star maps where stars are represented by points on a pla ...
- CART剪枝
与上篇文章中提到的ID3算法和C4.5算法类似,CART算法也是一种决策树分类算法.CART分类回归树算法的本质也是对数据进行分类的,最终数据的表现形式也是以树形的模式展现的,CART与ID3,C4. ...
- 关于box-sizing
http://www.zhangxinxu.com/css3/css3-box-sizing.php box-sizing:border-box; -o-box-sizing:border-box; ...
- c# aes 加密解密
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- mysql----用户root被删除或忘记root密码的解决方案
修改文件my.cnf,可用VIM打开,如:sudo vim /etc/my.cnf 在[mysqld]下加上一行: skip-grant-tables 保存文件,然后重启mysqld程序:sudo s ...
- UI设计(流程/界面)设计规范
1.UI设计基本概念与流程 1.1 目的 规范公司UI设计流程,使UI设计师参与到产品设计整个环节中来,对产品的易用性进行全流程负责,使UI设计的流程规范化,保证UI设计流程的可操作性. 1.2范围 ...
- Best Meeting Point 解答
Question A group of two or more people wants to meet and minimize the total travel distance. You are ...
- 【HDU1272】小希的迷宫(并查集基础题)
仍旧裸敲并查集.有这两点注意: 1.输入 0 0 时候要输出YES 2.留心数组的初始化 #include <iostream> #include <cstring> #inc ...
- Spark函数详解系列之RDD基本转换
摘要: RDD:弹性分布式数据集,是一种特殊集合 ‚ 支持多种来源 ‚ 有容错机制 ‚ 可以被缓存 ‚ 支持并行操作,一个RDD代表一个分区里的数据集 RDD有两种操作算子: ...
- poi读写Excel文件
jxl 只有excel基本的操作,代码操作比较方便,一般使用jxl就够了,对图片支持较好 poi功能比jxl强大但是比较吃内存,支持计算公式 关于jxl具体可以参考 http:// ...