Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extra parentheses on function [duplicate]

Possible Duplicate:
What do parentheses surrounding a JavaScript object/function/class declaration mean?
What does this “(function(){});”, a function inside brackets, mean in javascript?
A Javascript function

I encountered markup similar to this:

var something = (function(){
    //do stuff
    return stuff;
})()
document.ondblclick = function(e) { alert(something(e)) };

I don't understand the opening ( and closing )() in the something variable. Could you explain the difference to writing it like this?

var something = function(){
    //do stuff
    return stuff;
};

Thanks!

like image 676
Sean Bone Avatar asked Aug 03 '12 12:08

Sean Bone


People also ask

How do we find a duplicate parentheses using stack?

The idea is to use stack. Iterate through the given expression and for each character in the expression, if the character is a open parenthesis '(' or any of the operators or operands, push it to the top of the stack.

What is double parentheses in JavaScript?

It means that the first function ( $filter ) returns another function and then that returned function is called immediately. For Example: function add(x){ return function(y){ return x + y; }; } var addTwo = add(2); addTwo(4) === 6; // true add(3)(4) === 7; // true.

What do the brackets after a function do?

In JavaScript, the functions wrapped with parenthesis are called “Immediately Invoked Function Expressions" or "Self Executing Functions. The purpose of wrapping is to namespace and control the visibility of member functions. It wraps code inside a function scope and decrease clashing with other libraries.

What do empty parentheses mean in Excel?

They show that you're dealing with functions here. VariableName+() is a function call and empty () mean that you don't use parameters here.

What is () after function in JavaScript?

Show activity on this post. (function () {}) creates an anonymous function. Adding the () to the end calls the function that was just created. In the case of this particular function, the anonymous function returns several properties to the Browser object.


2 Answers

It's probably easier to understand if you leave the redundant parens out because they serve no purpose:

var something = function() {
    return 3;
} // <-- a function. 
(); // now invoke it and the result is 3 (because the return value is 3) assigned to variable called something 

console.log(something) //3 because the function returned 3 

var something = function() {
    return 3;
}; // a function is assigned to a variable called something

console.log(something) //logs the function body because it was assigned to a function
console.log(something()) //invoke the function assigned to something, resulting in 3 being logged to the console because that's what the function returns
like image 152
Esailija Avatar answered Oct 22 '22 12:10

Esailija


(function(){ ... }) is a (anonymous) function expression, you could e.g. assign that value to a variable.

The brackets behind it will immidiately execute the function expression, resulting in the return value of the function (in here: stuff). The construct is called IIFE.

When stuff is a function (which I assume, because you invoke something lateron), this is called a closure - the returned function (stuff, assigned to something) still has access to the variables in the execution context of that anonymous function.

like image 21
Bergi Avatar answered Oct 22 '22 10:10

Bergi