+ 3

Why these brackets?

Without the last pair of brackets the log won't show. Why? var getReady = function() { log('Ready for action'); }();

30th Dec 2016, 1:49 PM
The Hunter
The Hunter - avatar
10 Answers
+ 5
Because you use an anonyme function... When you declare a function, you write: function functionName() { /* code of your function */ } This is the same, in fact, that: functionName = function() { /* code */ } Which assign the function to the variable "functionName"... Well, to call it, in the two case ( because we keep the same name ), you do: functionName(); So, in your code, you assign at the variable getReady, the value returned by execution of a function without name: you declare it, and execute it once, just after have declared it ( and there is no more way to call it a second time in this case -- sometimes, you declare anonymized function, without explicit assignment to a variable, but you register it even, like if passing to a function, which provide an implicit assignement to the attribute who's a function variable )
30th Dec 2016, 4:21 PM
visph
visph - avatar
+ 4
About the not-question of the log() function: nothing indicates that it's not a custom function, making go know what ^^
30th Dec 2016, 5:15 PM
visph
visph - avatar
+ 3
if you leave the last brackets it will just declare a function if you put last brackets too it calls the function itself
30th Dec 2016, 4:57 PM
Sandeep Chatterjee
+ 3
@Seiss K said "brackets" but context shows implicitly it was meaning "parenthesis" ;)
31st Dec 2016, 9:20 AM
visph
visph - avatar
+ 1
if u don't place brackets it stores tha function in that variable ex: var m1 = function(){----};// function stored as an object in m1 m1();// this executes above function if u place the brackets it executes the function and store the output value of that function ex: var m1 = function(a,b) { return a+b;}(2,3); alert(m1);// output is 5 here here we are creating anonymous function with param's and passing 2,3 as parameters. that function return value stored in m1.
14th Feb 2017, 11:21 AM
dathatreya
0
I am not sure but maybe you should type 'console.log' instead of just 'log'. I have absolutly no idea what the last parenthesis are about
30th Dec 2016, 2:07 PM
coreplo
coreplo  - avatar
0
Thats it. Console.log() not lo()
30th Dec 2016, 2:27 PM
Ty E
0
now, that's a good answer! Thumbs up for visph!
30th Dec 2016, 5:06 PM
coreplo
coreplo  - avatar
0
the brackets r used for declaring a block of code
31st Dec 2016, 7:12 AM
Shahriar Adib
Shahriar Adib - avatar
0
Because you need to "call" function execution. Eather with some parameter inside brackets or empty brackets if you don't have parameter...
2nd Jan 2017, 8:56 AM
Branislav