当前位置: 首页 > news >正文

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

在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指代的是第二个参数john
func();
//输出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!

转载于:https://www.cnblogs.com/shinehouse/articles/3853296.html

相关文章:

  • 距离变换DT
  • 2-3. 逆序的三位数(10)
  • 发布/订阅消息传送模型
  • android 永不关闭toast
  • ZOJ 2770 Burn the Linked Camp(spfabellman)
  • Frontend Development
  • hdoj 1686 kmp
  • CCS教程
  • windows安装配置mongodb及图形工具MongoVUE
  • iOS中的NSBundle常用方法
  • JAVA static 作用
  • Android 创建项目时出现appcompat_v7 类包的问题
  • Java 定时循环运行程序
  • 微信分享自定义标题和图片的js
  • delphi 使用进度条查看浏览器状态
  • 「译」Node.js Streams 基础
  • 【RocksDB】TransactionDB源码分析
  • 【知识碎片】第三方登录弹窗效果
  • Fabric架构演变之路
  • HTML-表单
  • Java多线程(4):使用线程池执行定时任务
  • python大佬养成计划----difflib模块
  • Spring-boot 启动时碰到的错误
  • SpriteKit 技巧之添加背景图片
  • Sublime text 3 3103 注册码
  • v-if和v-for连用出现的问题
  • Webpack 4 学习01(基础配置)
  • 更好理解的面向对象的Javascript 1 —— 动态类型和多态
  • 机器学习学习笔记一
  • 利用jquery编写加法运算验证码
  • 面试遇到的一些题
  • 前端面试之CSS3新特性
  • 浅谈JavaScript的面向对象和它的封装、继承、多态
  • 数据可视化之 Sankey 桑基图的实现
  • 小程序button引导用户授权
  • 职业生涯 一个六年开发经验的女程序员的心声。
  • #调用传感器数据_Flink使用函数之监控传感器温度上升提醒
  • (动手学习深度学习)第13章 计算机视觉---图像增广与微调
  • (附源码)springboot 个人网页的网站 毕业设计031623
  • (附源码)ssm码农论坛 毕业设计 231126
  • (顺序)容器的好伴侣 --- 容器适配器
  • (算法)前K大的和
  • (转)平衡树
  • (轉)JSON.stringify 语法实例讲解
  • .NET 事件模型教程(二)
  • .net和jar包windows服务部署
  • .pyc文件还原.py文件_Python什么情况下会生成pyc文件?
  • /etc/shadow字段详解
  • @angular/cli项目构建--http(2)
  • @RestController注解的使用
  • @SentinelResource详解
  • @Transient注解
  • [AIGC] SQL中的数据添加和操作:数据类型介绍
  • [Angularjs]asp.net mvc+angularjs+web api单页应用之CRUD操作
  • [C#]扩展方法