All scripts into 1 file? Or spread out? Also global vars

stickyNav without parenthesis appears to work. Bit confused as to the logic (why it doesn’t having parenthesis makes it work…I’d assume I’d NEED parenthesis…)

If you use parenthesis you’re executing the function on the spot and passing the return value (functions that don’t explicitly return anything will return undefined) as the argument to addEventListener. Leaving off the parenthesis passes the function as an argument, to be called later (when the event is triggered).

1 Like

Ah, ok thank you! That clears things up.

The second parameter of an addEventListener needs to be a function.

You can either specify the function directly by name eg stickyNav or you can specify an anonymous function to be run function() {stickyNav()}

If you specify stickyNav() as that parameter then stickyNav needs to return the function that is to be run when the event is triggered as stickyNav() will run straight away and substitute whatever it returns as the code at that point.

Right, and if I understand right, then it will return undefined, in this case, and that obviously isn’t a function (and addEventListener needs a function to run?)

you do.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.