bind/on in JavaScript, jQuery, Backbone, Underscore

  • jQuery

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

alert( "User clicked on 'foo.'" );prototype

});
on的別名ip

  • Backbone

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

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

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

object在extend Backbone.Events後得到bind和trigger自定義名稱的events的能力。
on的別名cli

  • Underscore

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

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

將function綁定到object,function調用時,this將指向object。co

bindAll _.bindAll(object, *methodNames)
用一系列的methodNames將methods綁定到object,function調用時將在object的上下文中。

  • JavaScript

Function.prototype.bind()

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

與其餘三種bind關係不大。

相關文章
相關標籤/搜索