本文参考:

http://blog.bigbinary.com/2011/08/18/understanding-bind-and-bindall-in-backbone.html

bindAll内部调用了bind(),而bind()又内部调用了apply()

apply()的作用

apply lets us control the value of this when the function is invoked.

apply可以改变函数function的上下文,也就是this的值

例子:

var func = function beautiful(){
alert(this + ' is beautiful'); //这里的this是全局变量 window
};
func();
var func = function beautiful(){
alert(this + ' is beautiful');
};
func.apply('Internet');// 修改this的值

为什么要改变this的值

因为js的变态,举个栗子

function Developer(skill) {
this.skill = skill;
this.says = function(){
alert(this.skill + ' rocks!');
}
}
var john = new Developer('Ruby'); //实例化一个Developer
john.says(); //调用says()方法 Ruby rocks!

在这种情况下 this是Developer对象。

但是,如果换下面的写法,就不是了。

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!

在这种情况下,func()是在全局上下文调用的,所以this就变成了全局变量window,window.skill因为没有定义,所以是undefined。

使用apply来修改:

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);

这种方式虽然解决了问题,但是每次调用时,都要传递“john”这个参数,不方便函数的调用。

In JavaScript world functions are first class citizens. The reason why we create function is so that we can easily pass it around. In the above case we created a function called func. However along with the function func now we need to keep passing john around. That is not a good thing. Secondly the responsibility of rightly invoking this function has been shifted from the function creator to the function consumer. That’s not a good API.

因此,我们需要使用bind。

使用bind来解决

function Developer(skill) {
this.skill = skill;
this.says = function(){
alert(this.skill + ' rocks!');
}
}
var john = new Developer('Ruby');
var func = _.bind(john.says, john); //bind有返回值,返回一个函数
func();// Ruby rocks!

To solve the problem regarding this issue we need a function that is already mapped to john so that we do not need to keep carrying john around. That’s precisely what bind does. It returns a new function and this new function has this bound to the value that we provide.

bind的内部实现:

return function() {
return func.apply(obj, args.concat(slice.call(arguments)));
};

bind(方法名, this上下文)

bind并没有修改原来的方法,而是返回一个新方法,这个新方法的this已经变成了第二个参数中赋予的值。

使用bindAll来解决

function Developer(skill) {
this.skill = skill;
this.says = function(){
alert(this.skill + ' rocks!');
}
}
var john = new Developer('Ruby');
_.bindAll(john, 'says');
var func = john.says;
func();

在使用bind时,必须使用bind返回的函数。

在使用bindAll时,不必考虑返回值。实质上,bindAll是改变了原来定义的函数。

在原来定义的Developer类中,有一个'says'属性,bindAll直接改变了'says'属性。

使用Backbone的常见错误

错误代码:

window.ProductView = Backbone.View.extend({
initialize: function() {
_.bind(this.render, this);
this.model.bind('change', this.render);
}
});

错误原因:bind的返回值没有被使用,所以_.bind(this.render, this);没有起作用

修改:

window.ProductView = Backbone.View.extend({
initialize: function() {
var func = _.bind(this.render, this);
this.model.bind('change', func);
//或者
// this.model.bind('change', _.bind(this.render, this));
}
});

或者使用bindAll

window.ProductView = Backbone.View.extend({
initialize: function() {
_.bindAll(this, this.render);
this.model.bind('change', this.render);
}
});

Backbone中bind和bindAll的作用的更多相关文章

  1. 理解 backbone.js 中的 bind 和 bindAll 方法,关于如何在方法中指定其中的 this,包含apply方法的说明[转载]

    转载自:http://gxxsite.com/content/view/id/132.html 在backbone.js的学习过程中,被bind和bindAll弄得有点晕,这里包括underscore ...

  2. jQuery 中bind(),live(),delegate(),on() 区别(转)

    当我们试图绑定一些事件到DOM元素上的时候,我相信上面这4个方法是最常用的.而它们之间到底有什么不同呢?在什么场合下用什么方法是最有效的呢? 准备知识: 当我们在开始的时候,有些知识是必须具备的: D ...

  3. 转 jQuery 中bind(),live(),delegate(),on() 区别

    当我们试图绑定一些事件到DOM元素上的时候,我相信上面这4个方法是最常用的.而它们之间到底有什么不同呢?在什么场合下用什么方法是最有效的呢? 准备知识: 当我们在开始的时候,有些知识是必须具备的: D ...

  4. jQuery中bind方法和live方法区别解析

    Javascript中的事件有它的独特性,有默认的执行事件,例如冒泡就是其中的一个.在很多比较复杂的应用程序中,停止事件的冒泡或捕获在程序开发当中是十分有用的,而在IE中有它的独特方式来阻止事件的冒泡 ...

  5. jquery实现input输入框实时输入触发事件代码 ---jQuery 中bind(),live(),delegate(),on() 区别

    复制代码 代码如下: <input id="productName" name="productName" value="" /> ...

  6. Jquery中bind(), live(), on(), delegate()四种注册事件的优缺点,建议使用on()

    jquery中注册的事件,注册事件很容易理解偏差,叫法不一样.我第一反应就是如何添加事件,使用onclick之类的,暂时不讨论js注册事件的方法. 也看到园内前辈写过相关的帖子,但不是很详细,我找到了 ...

  7. Redis配置文件中bind参数

    前言 我们都知道,redis 的配置文件中,默认绑定接口是 127.0.0.1,也就是本地回环接口,所以是无法从外网连接 redis 服务的.如果想要让外网也能连接使用服务器上的 redis 服务,可 ...

  8. 面试官:能解释一下javascript中bind、apply和call这三个函数的用法吗

    一.前言    不知道大家还记不记得前几篇的文章:<面试官:能解释一下javascript中的this吗> 那今天这篇文章虽然是介绍javascript中bind.apply和call函数 ...

  9. Backbone中的model和collection在做save或者create操作时, 如何选择用POST还是PUT方法 ?

    Model和Collection和后台的WEB server进行数据同步非常方便, 都只需要在实行里面添加一url就可以了,backbone会在model进行save或者collection进行cre ...

随机推荐

  1. 图像描点标注-labelme的安装及使用

    1.直接使用pip安装lebelme pip install labelme 2.labelme的使用 找到labelme的安装路径,先找到python的安装路径如我的,C:\Users\Think\ ...

  2. 磁盘IO性能优化-实践

    RAID卡缓存策略调整 原因详解 操作实例 I/O 调度算法 文件系统journal 磁盘挂载参数 操作实例 性能数据对比 RAID卡缓存策略调整 可以将RAID卡缓存策略由No Write Cach ...

  3. Halcon WPF C#采集图像区域灰度值

    源码下载地址:https://github.com/lizhiqiang0204/ImageGray.git Halcon代码如下: *读取图片,转换成灰度图片 read_image (Image1, ...

  4. JS实现网页飘窗

    1.在html中设置一个飘窗的div,div中可以添加图片,添加文字通过css展现在飘窗中: <!--飘窗--> <div id="roll"> <i ...

  5. 大数减法(A - B Problem Plus)问题

    解题思路 flagA为0表示A为正整数,为-1表示A为负整数: flagB为0表示B为正整数,为2表示B为负整数: 而 flag = flagA + flagB. 1.当 flag == 0 表示数A ...

  6. 【bzoj3676】[Apio2014]回文串

    *题目描述: 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的“出现值”为t在s中的出现次数乘以t的长度.请你求出s的所有回文子串中的最大出现值. *输入: 输入只有一行,为一个只包含小 ...

  7. 使用xshell远程连接Linux

    Linux系统对于程序员来说并不陌生,对IT技术员来说是一个很好的开发平台,因此掌握Linux系统的操作对于一个程序员来说非常有用.而对于习惯使用windows的人来说直接在Linux系统下进行操作感 ...

  8. shell scripts 编写基础

    一.shell变量的相关用法: 变量作为被赋值的一方的时候不加$,只有在使用其值的内容的时候需要加上$,该符号可 1,变量中的单引号‘’.双引号“”“.反单引号‵`.括号().大括号{}.双括号(() ...

  9. JavaSE知识点:finalize,treeMap

    *)finalize finalize是Object类的一个方法,在垃圾收集器执行的时候会调用被回收对象的此方法,可以覆盖此方法提供垃圾收集时的其他资源回收,例如关闭文件等. *)treeMap 参考 ...

  10. tree 命令

    LMXMN117:Mac Driver will.wei$ tree -N >/tmp/savs.txt       (1)tree -a 显示所有文件和目录   (2)tree -d 显示目录 ...