简体   繁体   English

在以下代码中,此对象如何在 addEventListener 方法上工作?

[英]How this object work on addEventListener method in following code?

See fiddle小提琴

I have following Html code :我有以下 Html 代码:

<table id="outside">
    <tr><td id="t1">one</td></tr>
    <tr><td id="t2">two</td></tr>
</table>

and Js code :和 Js 代码:

var el = document.getElementById("outside");
var Something = function(element) {
  this.name = 'Something Good';
  this.onclick1 = function(event) {
    console.log(this.name); // undefined, as this is the element
  };
  this.onclick2 = function(event) {
    console.log(this.name); // 'Something Good'
  };
  el.addEventListener('click', this.onclick1, false);
  el.addEventListener('click', this.onclick2.bind(this), false); 
}
Something();

while execution of Something() , this refer to the Window object.在执行Something()this指的是Window对象。 so onclick1 and onclick2 is the method on Window object.所以onclick1onclick2Window对象上的方法。

Doubt :怀疑 :

  1. When el.addEventListener('click', this.onclick1, false);el.addEventListener('click', this.onclick1, false); is executed this object in this.onclick1 refer to the el object.执行this对象在this.onclick1引用el对象。 then why onclick1 method is called(which is a method of window object), when an event is triggred.那么为什么在onclick1事件时调用onclick1方法(这是一个window对象的方法)。 onclick1 method should call on window object not on el object. onclick1方法应该调用window对象而不是el对象。
  2. In el.addEventListener('click', this.onclick2.bind(this), false);el.addEventListener('click', this.onclick2.bind(this), false); How this object in this.onclick2 and onclick2.bind(this) is different? this.onclick2onclick2.bind(this) this对象this.onclick2不同?

This example is taken from MDN这个例子取自MDN

Event handlers are passed the element of the event target as the this object.事件处理程序将事件目标的元素作为this对象传递。

Function.bind() forces the this object of the called function to be a particular object, the parameter to bind. Function.bind()强制被调用函数的this对象成为特定对象,即要绑定的参数。

If you don't override this with fn.bind(thisObject) when you create the handler function, then it will default to the event target (as mentioned in the MDN link in your question).如果不重写thisfn.bind(thisObject)当您创建处理函数,然后它会默认为活动对象(如你的问题在MDN链接提到)。 If you do, it will be whichever object you specified as a parameter to .bind() .如果这样做,它将是您指定为.bind()参数的任何对象。

Additional info about .bind() : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind关于.bind()其他信息: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 一个对象的方法上的addEventListener - addEventListener on a method of an object 对象# <Object> 没有方法&#39;addEventListener&#39; - Object #<Object> has no method 'addEventListener' addEventListener方法是文档对象的一部分吗? - Is the addEventListener method part of the document object? 对象# <Controller> 没有方法addEventListener - Object #<Controller> has no method addEventListener 如何在addEventListener()调用中调用对象方法? - How do you call a object method from within an addEventListener() call? 对象[object Object]没有方法&#39;addEventListener&#39; - Object [object Object] has no method 'addEventListener' 使用reduce从数组中取平均值。 将您自己的方法添加到数组 object 以便以下代码可以工作。 3 种方法 - Average from array using reduce. Add your own method to the Array object so the following code would work. 3 approaches 如何使以下代码与流js注释一起使用? 使用Object.create() - How to make the following code work with flow js annotations? Using Object.create() 移位功能如何在以下代码中起作用? - How does shift function work in the following code? 如何修复无法正常工作的addEventListener? - How to fix addEventListener that does not work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM