码迷,mamicode.com
首页 > 编程语言 > 详细

bind/on in JavaScript, jQuery, Backbone, Underscore

时间:2015-09-13 13:11:34      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

  • jQuery

$( "#foo" ).bind( "click", function() {

alert( "User clicked on ‘foo.‘" );

});
on的别名

  • Backbone

var object = {};
_.extend(object, Backbone.Events);

object.on("alert", function(msg) {
alert("Triggered " + msg);
});

object.trigger("alert", "an event");

object在extend Backbone.Events后获得bind和trigger自定义名称的events的能力。
on的别名

  • Underscore

bind
_.bind(function, object, *arguments)

var func = function(greeting){ return greeting + ‘: ‘ + this.name };
func = _.bind(func, {name: ‘moe‘}, ‘hi‘);
func();
=> ‘hi: moe‘

将function绑定到object,function调用时,this将指向object。

bindAll _.bindAll(object, *methodNames)
用一系列的methodNames将methods绑定到object,function调用时将在object的上下文中。

  • JavaScript

Function.prototype.bind()

fun.bind(thisArg[, arg1[, arg2[, ...]]])

与其他三种bind关系不大。

bind/on in JavaScript, jQuery, Backbone, Underscore

标签:

原文地址:http://www.cnblogs.com/wenkw/p/4804588.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!