And then, I tried the code below. It did not come out as I expected…
The alert window popped out right after the page was loaded, and then when I clicked the button, nothing happened …
What you are doing is immediately running that counter.inc() function and assigning any returned value to the click handler. Instead, remove the () to assign the function itself:
And that will run the alert as you expect. However, you will encounter some strangeness, as the this keyword in the function may not be what you expect - you’re calling counter.inc from within the context of a click handler, so this will refer to that click handler.
A great book series, You Don’t Know JavaScript (Yet), talks about that in it’s section on Scope & Context.
This is another puzzle I have in my head… when to pass/use “function” and when to pass/use “function()”… what is the best way to understand this? since I have seen many cases where “function” was passed to a method/function and sometimes “function()” is passed instead.
Thank you for the book. I was told about this book but have been so busy and procrastinated. I definitely need to read it.
So, it is a challenging topic, but i think we can make some sense of it here.
The first thing to grasp is that functions, like strings or numbers or arrays, are simply data. They are things we can assign to a variable, and reference by that variable at any point. We can use them, or we can pass them into or out of function calls. Javascript sees absolutely no difference, just another kind of value.
This is what is meant when we refer to functions as first-class members of the language. They receive no special treatment, they’re just data.
So anytime we wish to refer to the function itself and not it’s evaluation, we can do so by simply providing the variable reference with no parentheses. When we provide a function to array.filter(...), for example, we are providing a reference yup
The first one will run popout after the timeout. The second will immediately run popout(), immediately display the alert, and after the timeout, there’s nothing for it to actually do.
setTimeout accepts a function, in this case popout, and will call it at the appropriate time
popout() is not a function, it’s the output of a function, in this case I think undefined - the function still has to execute to have the output so you still have the alert, but it executes before the timer
I see. Thank you for the explanation.
From what I understand now, it seems that popout is being treated as a reference to the function while popout() is being treated as an expression that produces a value or it behaves more like a callback function.
somewhat yes, but not the last part, as setTimeout takes a callback to call after a timer
popout points in memory to the function, and is being called by the method after the appropriate time
and popout() is the expression to call a function, when you put the parenthesis after a function name, you are saying “put here the output of the function”