- 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关系不大。
时间: 2024-10-07 13:57:45