Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the meaning of "()" in a function call?

Tags:

javascript

Now, I usually call a function (that requires no arguments) with () like this:

myFunction(); //there's empty parens

Except in jQuery calls where I can get away with:

$('#foo').bind('click', myFunction); //no parens

Fine. But recently I saw this comment here on SO:

"Consider using setTimeout(monitor, 100); instead of setTimeout('monitor()', 100);. Eval is evil :)"

Yikes! Are we really eval()-ing a string here? I guess I don't really understand the significance and implications of 'calling' a function. What are the real rules about calling and referring to functions?

like image 715
Isaac Lubow Avatar asked Sep 04 '10 07:09

Isaac Lubow


People also ask

What happens when func () is called?

Now, whenever a function is called a new stack frame is created with all the function's data and this stack frame is pushed in the program stack, and the stack pointer that always points the top of the program stack points the stack frame pushed as it is on the top of the program stack.

What does call () do in JavaScript?

The call() allows for a function/method belonging to one object to be assigned and called for a different object.

What is the meaning of function call?

A function call is an expression that passes control and arguments (if any) to a function and has the form: expression (expression-listopt) where expression is a function name or evaluates to a function address and expression-list is a list of expressions (separated by commas).

What is function call example?

Example 1: Using call() Methodfunction sum(a, b) { return a + b; } // invoking sum() by passing this and 'a', 'b' arguments let result = sum.call(this, 5, 3); console.log(result); Run Code. Output: 8. In the above example, we have defined a function sum() that returns the sum of two numbers.


1 Answers

In JavaScript functions are first-class objects. That means you can pass functions around as parameters to a function, or treat them as variables in general.

Let's say we are talking about a function hello,

function hello() {
    alert('yo');
}

When we simply write

hello

we are referring to the function which doesn't execute it's contents. But when we add the parens () after the function name,

hello()

then we are actually calling the function which will alert "yo" on the screen.

The bind method in jQuery accepts the type of event (string) and a function as its arguments. In your example, you are passing the type - "click" and the actual function as an argument.

Have you seen Inception? Consider this contrived example which might make things clearer. Since functions are first-class objects in JavaScript, we can pass and return a function from within a function. So let's create a function that returns a function when invoked, and the returned function also returns another function when invoked.

function reality() {
    return function() {
        return function() {
            alert('in a Limbo');
        }
    };
}

Here reality is a function, reality() is a function, and reality()() is a function as well. However reality()()() is not a function, but simply undefined as we are not returning a function (we aren't returning anything) from the innermost function.

So for the reality function example, you could have passed any of the following to jQuery's bind.

$('#foo').bind('click', reality);
$('#foo').bind('click', reality());
$('#foo').bind('click', reality()());
like image 199
Anurag Avatar answered Sep 28 '22 10:09

Anurag