admin管理员组文章数量:1337125
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 beWindow
but what would be the currentTarget (base) on which the handler is assigned (registered) – Jake Commented Apr 22, 2017 at 11:04
1 Answer
Reset to default 7It 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( ... );
本文标签: javascriptaddEventListener() as a global functionStack Overflow
版权声明:本文标题:javascript - addEventListener() as a global function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742365070a2461125.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论