Reputation: 29703
I have next function
var hideAll = function() {
// code
return ///...
};
And I am using this function like callback in another function.
When I am using it like
function (params, hideAll) {}
all working well, but when I am using
function (params, hideAll() ) {}
all not working well!
So my question is, what is difference between hideAll
and hideAll()
function executions?
Upvotes: 2
Views: 1158
Reputation: 28095
Let's step back for a moment and assume you would call a method foo
first like this:
foo(params, hideAll() ) {}
This uses the return value of hideAll as the value
whereas
foo(params, hideAll) {}
Uses the hideAll function itself as the value
However, what you actually tried to do is to declare a function like this:
function (params, hideAll() ) {}
That makes no sense. You can't declare a function with anything different from a parameter in the parameter list.
So while both forms are legal for function invocation (still with a total different meaning) the latter isn't legal for function declaration
Upvotes: 0
Reputation: 114579
In Javascript and in many other languages functions are "first class objects" and this means that you can call/execute a function but you can also store the function in a variable or in an array, or you can pass a function to another function.
Note that I'm not talking about passing the value resulting from calling a function... but the function itself. Consider:
function test10(f) {
for (var i=0; i<10; i++)
alert(f(i));
}
function square(x) { return x*x*; }
function cube(x) { return x*x*x; }
test10(square);
test10(cube);
The last two lines are passing a function (square
and cube
) as a parameter to function test10
.
The ()
syntax tells Javascript that you want to make the call, and can be used not only with function names, but with any expression like variables or array elements... for example:
var f_arr = [square, cube];
for (var i=0; i<2; i++)
alert(f_arr[i](i+42)); // Will call square(42) and cube(43)
Actually in Javascript the code
function square(x) {
return x * x;
}
is not identical but similar to
square = function(x) {
return x * x;
};
so defining a function is indeed close to assigning a variable
Upvotes: 0
Reputation: 11264
hideAll
- this is a reference to the function
hideAll()
- this is execution of the function, its result
function (params, hideAll) {}
is a correct function definition,
whereas function (params, hideAll() ) {}
is not - you are unable to call another function in function definition.
However you could still write the following valid code:
var hideAll = function() {
// code
return ///...
};
var functionWithCallback = function(callback){
callback();
}
var closureReferringHideAll = function(){
hideAll();
}
// The following two lines will do exactly the same in current context,
// i.e. execute hideAll.
functionWithCallback(hideAll);
closureReferringHideAll();
Upvotes: 3
Reputation: 2000
hideAll() is not a function, it is result of execution function.
function hideAll(){
return 0;
}
hideAll() - number 0
hideAll - function
Upvotes: 0