I was going through a Svelte tutorial where they bind a function to on:click
in a button and they show how if you put ()
after the function name it doesn't work right, it runs once when the page loads and then when you click the button the function isn't ran. They said to remove the ()
from the function name so that you bind to a pointer to the function instead of the function itself. If you do that then it works as expected. I did further research and it looks like pointers to functions in JavaScript don't really exist. So what's going on here? Do pointers to functions exist or do they not exist, and what's the difference in Svelte when binding to a function that has ()
(what's that called?) after its name and a function that doesn't?
Asked
Active
Viewed 35 times
0

JSNinja
- 133
- 1
- 9
-
The function name / identifier essentially points to the function. Using the function name anywhere results in the environment looking up what that name points to, and retrieving that value. That's how *all* variables in JavaScript work, not just functions. – CertainPerformance Oct 30 '21 at 20:05
-
Is a function's value what it returns in a `return` statement, or is it everything inside the function? – JSNinja Oct 30 '21 at 20:24
-
1By "retrieving that value", I mean "the value that the name is connected to", which is the entire function - not what the function returns, but the function itself. – CertainPerformance Oct 30 '21 at 20:43