现在的位置: 首页 > 综合 > 正文

prototype bind 和 bindAsEventListener的区别

2013年10月13日 ⁄ 综合 ⁄ 共 973字 ⁄ 字号 评论关闭

prototype.js代码
Function.prototype.bind = function() {
  var __method = this, args = $A(arguments), object = args.shift();
  return function() {
    return __method.apply(object, args.concat($A(arguments)));
  }
}

Function.prototype.bindAsEventListener = function(object) {
  var __method = this;
  return function(event) {
    return __method.call(object, event || window.event);
  }
}

实例代码
function class(aa){
  this.aa = aa;
}
class.prototype.show = function(arg1,arg2){}
class.prototype.close = function(){}

bind可以绑定多个参数

Event.observe(menuItem,this.eventType,this.show.bind(this,arg1,arg2))

bindAsEventListener只可以绑定一个this参数

Event.observe(menuItem,this.eventType,this.show.bindAsEventListener(this))

【上篇】
【下篇】

抱歉!评论已关闭.