最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - addEventListener() as a global function - Stack Overflow

programmeradmin1浏览0评论

I just caught up in a confusion.
if suppose I implement addEventListener() as a global function (unlike as a method of some specific node like node.addEventListener() ) then does it act just like a usual global function or something goes under the hood while executing the code ending up being a method of some specific node

Note: DOM level 2, which defined the addEVentListener, stipulates that handler gets registered to the node. so which node is it registered to; window object is not a node

I just caught up in a confusion.
if suppose I implement addEventListener() as a global function (unlike as a method of some specific node like node.addEventListener() ) then does it act just like a usual global function or something goes under the hood while executing the code ending up being a method of some specific node

Note: DOM level 2, which defined the addEVentListener, stipulates that handler gets registered to the node. so which node is it registered to; window object is not a node

Share Improve this question edited Apr 22, 2017 at 16:38 Jake asked Apr 22, 2017 at 10:34 JakeJake 2542 silver badges17 bronze badges 4
  • Yes, addEventListener is a native method that is prototyped on every EventListener-patible object, creating a global function with the same name makes no difference – adeneo Commented Apr 22, 2017 at 10:47
  • @adeneo but DOM level 2, which defined the addEVentListener, stipulates that handler gets registered to the node. so which node is it registered to; window object is not a node – Jake Commented Apr 22, 2017 at 10:54
  • The window object represents a window containing a DOM document, it's not a "node" per se, but the spec says that the event target that addEventListener is attached to may be an Element in a document, the Document itself, a Window, or any other object that supports events (such as XMLHttpRequest). – adeneo Commented Apr 22, 2017 at 11:00
  • Oops thankx for bringing this into the light that event target can be Window, but still there remains something.. event target can be Window but what would be the currentTarget (base) on which the handler is assigned (registered) – Jake Commented Apr 22, 2017 at 11:04
Add a ment  | 

1 Answer 1

Reset to default 7

It will applies to the global object window (which have the function addEventListener). Because:

var a = 5;

console.log(a);
console.log(window.a);

Thus:

addEventListener( ... );

is the exact same things if you use:

window.addEventListener( ... );
发布评论

评论列表(0)

  1. 暂无评论