javascript syntax explanation -


possible duplicate:
what mean? (function (x,y)){…}){a,b); in javascript

please explain , give example javascript below, can't find document explain this:

(function([arg, arg ...]) {      //javascript code here  })([str, str ...]); 

i'm asking question because see in google facebook javascript code, of them using syntax.

share|improve question

marked duplicate felix kling, richard everett, matt, gordon, chrisf nov 14 '11 @ 17:16

this question has been asked before , has answer. if answers not address question, please ask new question.

1  
search javascript closures. blog.morrisjohns.com/javascript_closures_for_dummies – jantimon oct 14 '11 @ 8:06
    
oh, ok try, thank ghommey :) – alwayz change oct 14 '11 @ 8:09
3  
i doubt searching on closures much. pattern called immediately invoked function expression some. others have similar names. can used create closures, isn't necessary create closure nor way. – robg oct 14 '11 @ 8:13
up vote 3 down vote accepted

what's happening is, anonymous function expression defined (in parentheses) -

(function([arg, arg ...]) {      //javascript code here  }) 

the function called using () @ end of function, as you'd call normal function var result = somefunction(). , example -

(function() {     alert('called "()" @ end of function'); })() 

you can pass arguments anoymous function, via parentheses -

(function(username) {     alert('called "()" @ end of function. hello ' + username + '!'); })('dave') 
share|improve answer
    
+1 - speed enemy of accuracy. :-) – robg oct 14 '11 @ 8:26
    
@robg - thanks! not sure can blame on speed though :) – ipr101 oct 14 '11 @ 8:52

it anonymous function invocation

the arguments values str, str passed function arguments arg, arg , javascript code executed.

it same as

function foo([arg, arg ...]) {     //javascript code here }  foo([str, str, ...]); 
share|improve answer
    
+1 getting right. :-) – robg oct 14 '11 @ 8:23
(function(a, b) {   var x; }) 

returns anonymous function. technical/historical reasons, anonymous functions must wrapped in parentheses.

notably, names a,b , x not visible outside code. in contrast, writing var x; make x accessible , thereby pollute global namespace. equivalent of private variables in classical object-orientated languages.

we write:

var func = function(a, b) {var x;}; // or equivalently: function foo(a, b) {var x;};  func('arg1', 2); 

but again pollute global namespace making function func visible else. since don't want call func more once anyway,

(function(a, b) {var x;}) ('arg1', 2); 

is perfect way execute code variables without making these variables(or any variable names) visible rest of world.

share|improve answer
    
the expression @ top of post function declaration no name, syntax error. given name make syntactically legal, has no return statement returns undefined, not function. – robg oct 14 '11 @ 8:21
    
@robg thanks. fixed: enclosed parentheses make syntactically legal, indeed returns function. note anonymous functions not need wrapped in parentheses in expressions, in statements. – phihag oct 14 '11 @ 8:23

the function in round brackets called once args defined strs.

the round brackets containing function used limit scope of function, i.e. function cannot called externally.

the str variables can global variables (e.g. jquery) , can aliased corresponding arg (e.g. jquery -> $) sole use of function.

generally, good, safe way write javascript, e.g.

(function($) {    // code })(jquery) 

for function, local variable '$' means global variable 'jquery'. means can have '$' defined else in script (or function) , variable not affect '$' in function.

share|improve answer
    
the round brackets containing function not scope operators. there allow function invoked immediately. if weren't there, syntax error occur. – matt oct 14 '11 @ 8:19
    
ah, point - thanks. although, point function cannot called externally (since it's anonymous) still true. – tomtom oct 14 '11 @ 8:21

not answer you're looking for? browse other questions tagged or ask own question.

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -