本文参考:

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. pandas进阶

    pandas是基于numpy构建的库,在数据处理方面可以把它理解为numpy的加强版,由于numpy主要用于科学计算,特长不在于数据处理,我们平常处理的数据一般带有列标签和index索引,这时pand ...

  2. Uva 10635 - Prince and Princess LCS/LIS

    两个长度分别为p+1和q+1的由1到n2之前的整数组成的序列,每个序列的元素各不相等,两个序列第一个元素均为1.求两个序列的最长公共子序列 https://uva.onlinejudge.org/in ...

  3. Linux加载一个可执行程序并启动的过程

    原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 作者:严哲璟 以shell下 ...

  4. FAT12 img tool

    NJU/2019/OS Description: CODE: Main.cpp: /* @author: Edwin Xu @Date:2019/11/13 @Note: just ASCII */ ...

  5. IO流一

    1 Java IO原理 I/O是input/output的缩写,IO技术是非常实用的技术,用于处理设备之间的数据传输.如读写文件,网络通讯等. Java程序中,对于数据的输入输出操作以“流Stream ...

  6. Java日期时间以及日期相互转换_java - JAVA

    文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 Java日期时间,以及相互转化,供大家参考,具体内容如下 package com.study.string; impor ...

  7. linux运维、架构之路-cobbler无人值守

    一.cobbler介绍 Cobbler是一个Linux服务器安装的服务,可以通过网络启动(PXE)的方式来快速安装.重装物理服务器和虚拟机,同时还可以管理DHCP,DNS等 1.主要功能:①Cobbl ...

  8. vue的生产环境dependencies 和开发环境devDependencies,二者的理解和区别

  9. idea上把项目推送到GitHub上

  10. 软件工程 in MSRA 黄金点游戏-第一次结对编程

    简单介绍 第一次结对编程,邹欣老师选择了一个博弈游戏作为题目.博弈论是一门非常有趣的学科.之前竞赛时接触的博弈论大部分都是存在均衡点/必胜策略的.像这次这种多人参与,没有完美策略,你方唱罢我登台的游戏 ...