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( ... );