bind/on in JavaScript, jQuery, Backbone, Underscore

  • 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关系不大。

posted on 2015-09-13 12:39  打铁的小木头  阅读(124)  评论(0)    收藏  举报

导航