为什么log事件对象的时候currentTarget是null?

This is an artifact of the way the Javascript console works when you log an object. The log doesn’t contain a copy of all the object properties, it just contains a reference to the object. When you click on the disclosure triangle, it then looks up all the properties and displays them.
这是JavaScript console 在log一个对象的机制造成的。log 没有包含对象的所有属性,它只包含了对这个对象的引用。当你点击展开时,他才会给你找那个对象的属性。

In this case, at the time you call console.log(e), there’s a DOM element in the currentTarget property. But sometime later, that property is reset to null for some reason. When you expand the event object, that’s what you see.
你的情况是,当调用console.log(e)时,currentTarget属性是有值的,但是过后这个值就被重置为null了。所以当你展开事件对象,看到的就是null

A simple example that demonstrates this is:
这里有一个例子:

var foo = { };
for (var i = 0; i < 100; i++) {
    foo['longprefix' + i] = i;
}
console.log(foo);
foo.longprefix90 = 'abc';

When you view the object in the console, you’ll see that foo.longprefix90 contains “abc”, even though it contained 90 at the time that console.log(foo) was called.
虽然在调用console.log(foo)的时候foo.longprefix90应该是90,但是当你在console里浏览object的时候你会看到foo.longprefix90abc

The demonstration needs to use an object with lots of properties. When it’s logging, it shows the first few properties that fit in the console, so those are in the early snapshot. Only properties after that display this anomalous behavior.
这个情况需要一个属性超多的对象,当它被log的时候,它展示头几个属性是当时的快照,后面的属性才会出现这个异常。

来源: https://stackoverflow.com/que…

    原文作者:ssshooter
    原文地址: https://segmentfault.com/a/1190000010734680
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞