首页 > 编程语言 > 详细

bind/on in JavaScript, jQuery, Backbone, Underscore

时间:2015-09-13 13:09:29      阅读:267      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!