jQuery $.on()方法和addEventListener改变this指向

标签(空格分隔): jQuery JavaScript


jQuery $.on()

jq的绑定事件使用$([selector]).on([types], [selector], [data], [fn], [one])方法;解绑事件使用off,但是解绑具体事件时候handler只能是具名函数。

在一个对象中,当我们想要在具名函数中用this访问当前对象的属性,可以从[data]参数传入,然后在具名函数中通过e.data来访问:

var obj = {
options: { a: 1 },
init: function() {
$(window).off('click', this._event);
$(window).on('click', { self: this }, this._event);
},
_event: function(e) {
var self = e.data.self;
console.log(self.options);
}
};

addEventListener

详细内容参见MDN

addEventListener兼容性

1. 通过bind(this)方法

var Foo = function (ele) {
this.name = 'This is foo';
this.onclickA = function (e) {
console.log(this.name); // undefined
};
this.onclickB = function (e) {
console.log(this.name); // This is foo
}; ele.addEventListener('click', this.onclickA, false);
ele.addEventListener('click', this.onclickB.bind(this), false);
}; new Foo(document.body);

2. 通过定制的函数handleEvent去捕获任意类型

var Bar = function (ele) {
this.ele = ele;
this.name = 'This is bar';
this.handleEvent = function (e) {
console.log(this.name);
switch (e.type) {
case 'click':
console.log('Trigger click...');
break;
case 'dblclick':
console.log('Trigger dblclick...');
break;
}
}; ele.addEventListener('click', this, false);
ele.addEventListener('dblclick', this, false);
};
Bar.prototype.remove = function () {
// 你也可以移除这些监听事件
this.ele.removeEventListener('click', this, false);
this.ele.removeEventListener('dblclick', this, false);
}; var bar = new Bar(document.body);
bar.remove();

3. 给EventListener传递一个函数,调用想要访问对应作用域对象的方法

但是这样做绑定的事件成为了匿名函数,是不能取消绑定的。

class SomeClass {
constructor() {
this.name = 'This is a class';
} register() {
const that = this;
window.addEventListener('keydown', function (ev) { return that.foo(ev); });
} foo(e) {
console.log(this.name);
switch (e.keyCode) {
case 65:
console.log('a');
break;
case 13:
console.log('enter');
break;
}
}
} const obj = new SomeClass();
obj.register();

随机推荐

  1. unity 渲染第二步

    先不要用 unity shader 提供给你的转换矩阵,看看屏幕上的图形,你会学到更多. --- <unity 渲染箴言> 假设你 create 了一个 cube,放在默认的位置,默认的 ...

  2. Ubuntu 16.04安装SecureCRT替代XShell

    XShell应该是最强大的,在Ubuntu下只有SecureCRT能实现跨平台(Linux/Windows/Mac),并且可以实现Tab的功能等.当然,还有其它的类似PuTTY这些.Windows下建 ...

  3. Ubuntu14.04下完美安装cloudermanage多种方式(图文详解)(博主推荐)

    说在前面的话 我的机器是总共4台,分别为ubuntucmbigdata1.ubuntucmbigdata2.ubuntucmbigdata3和ubuntucmbigdata4. ClouderaMan ...

  4. Zookeeper概念学习系列之zab协议

    不多说,直接上干货! 上一章讨论了paxos算法,把paxos推到一个很高的位置. Zookeeper概念学习系列之paxos协议 但是,paxos有没有什么问题呢?实际上,paxos还是有其自身的缺 ...

  5. 403 for URL: http://www.terracotta.org/kit/reflector

    前面因为在一个项目中使用了ehcache.xml配置文件,后面启动tomcat的时候报下面的错误 java.io.IOException: Server returned HTTP response ...

  6. 关于Stream的Read方法

    一次做到一个关于使用DataContractJsonSerializer类的表述.其中需要用到MemoryStream数组读取.发生数组溢出错误,这里特记录一笔: public static clas ...

  7. Win7与Ubuntu双系统安装过程

    Win7安装1.宏基安装Win7插入Win7系统光盘-> 重新启动-> 按F12-> 选择CD安装-> 按任意键-> 选择自定义(高级),接下去根据提示安装. 2.华硕安 ...

  8. VMware workstation 非正常关机导致开机失败,解决方法

    问题:VMware workstation 非正常关机导致开机失败!如下图:

  9. Maven 配置Tomcat

    1.Tomcat conf 下的tomcat-users.xml 增加 <role rolename="manager"/> <role rolename=&qu ...

  10. Cardinality Estimation算法学习(二)(Linear Counting算法、最大似然估计(MLE))

    在上篇,我了解了基数的基本概念,现在进入Linear Counting算法的学习. 理解颇浅,还请大神指点! http://blog.codinglabs.org/articles/algorithm ...