Need help understanding function calls in JavaScript
javascript - Calling vs invoking a function - Stack Overflow
javascript - Defining and calling function in one step - Stack Overflow
Calling functions within another function
Videos
I am not sure but from what I understand JS functions can be invoked without anyone calling them, they call themselves?
Example from the built-in 'map()' method:
const array1 = [1, 4, 9, 16]; // pass a function to map const map1 = array1.map(x => x * 2); console.log(map1); // expected output: Array [2, 8, 18, 32]
the x = > x * 2 is an arrow function. The map iterates it. But I am used to calling functions from other languages. For example PHP when you declare a function first:
function helloWorld() {
echo "Hello world!";
}
helloWorld();But in JS functions are called when declared?
Your reference text:
It is common to use the term "call a function" instead of "invoke a function" ... In this tutorial, we will use invoke, because a JavaScript function can be invoked without being called.
Now let me rephrase it:
It is common to use the term "call a function" instead of "invoke a function" ... In this tutorial, we will use the term invoke instead of call, because a JavaScript function can be invoked indirectly like
fn.call()andfn.apply()without being called directly likefn().
So, when I do fn(), it's invoked directly and when I do it like fn.call(), it's invoked indirectly but in both cases, the function is being invoked. Otherwise, I see no difference here and I can also say that I can call a function in many ways, for example:
fn(); // I'm calling it
fn.call(); // I'm calling it
fn.apply(); // I'm calling it
So, the difference is semantic but both are interchangeable, IMO. BTW, I wrote a comment above, under the question and I would like to put it here, which is:
IMO, that's a misleading statement. Maybe there are some indication of call/apply or something else but it's totally confusing.
The difference is semantic and subtle. When you call a function, you are directly telling it to run. When you invoke a function, you are letting something run it.
There is one way to call a function:
myFunction()
Here, you are invoking the function (letting it run) by calling it directly.
There are many ways to invoke a function (given throughout different comments and answers). Here's one example:
function invoker(functionName) {
functionName()
}
invoker(myFunction)
Here, by calling invoker, you are invoking myFunction, which is being called indirectly.
You can try:
(window.powers = function(i) {
/*Code here*/
alert('test : ' + i);
})(2);
<a href="#" onclick="powers(654)">Click</a>
Working link : http://jsfiddle.net/SqBp8/
It gets called on load, and I have added it to an anchor tag to change the parameter and alert.
If all you want is access the function within its own body, you can simply specify a name after the function keyword:
> (function fac (n) {
return (n === 0 ? 1 : n*fac(n-1));
})(10)
3628800
This is a standard feature (see ECMA-262, ed. 5.1, p. 98).