2

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?

1
  • hideAll is a variable which is reffering to function. and hideAll(); is execution of function. Commented Jul 27, 2012 at 7:20

4 Answers 4

3

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();
Sign up to request clarification or add additional context in comments.

Comments

0

hideAll() is not a function, it is result of execution function.

function hideAll(){
    return 0;
}

hideAll() - number 0

hideAll - function

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.