Is there a difference between (function() {…}()); and (function() {…})();? [duplicate]

There is no practical difference in those two forms, but from a grammatical point of view the difference between the two is that The Grouping Operator – the parentheses – will hold in the first example a CallExpression, that includes the FunctionExpression: CallExpression | | FunctionExpression | | | V V (function() { }()); ^ … Read more

javascript: recursive anonymous function?

You can give the function a name, even when you’re creating the function as a value and not a “function declaration” statement. In other words: (function foo() { foo(); })(); is a stack-blowing recursive function. Now, that said, you probably don’t may not want to do this in general because there are some weird problems … Read more

What does this “(function(){});”, a function inside brackets, mean in javascript? [duplicate]

You’re immediately calling an anonymus function with a specific parameter. An example: (function(name){ alert(name); })(‘peter’) This alerts “peter“. In the case of jQuery you might pass jQuery as a parameter and use $ in your function. So you can still use jQuery in noConflict-mode but use the handy $: jQuery.noConflict() (function($){ var obj = $(‘<div/>’, … Read more

Reason behind this self invoking anonymous function variant

By default, invoking a function like (function(){/*…*/})() will set the value of this in the function to window (in a browser) irrespective of whatever the value of this may be in the enclosing context where the function was created. Using call allows you to manually set the value of this to whatever you want. In … Read more

Why do arrow functions not have the arguments array? [duplicate]

Arrow functions don’t have this since the arguments array-like object was a workaround to begin with, which ES6 has solved with a rest parameter: var bar = (…arguments) => console.log(arguments); arguments is by no means reserved here but just chosen. You can call it whatever you’d like and it can be combined with normal parameters: … Read more

How does a function in a loop (which returns another function) work? [duplicate]

When you assign the function to the click handler, a closure is created. Basically a closure is formed when you nest functions, inner functions can refer to the variables present in their outer enclosing functions even after their parent functions have already executed. At the time that the click event is executed, the handler refers … Read more