Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are "(function ( ) { } ) ( )" and "(function ( ) { } ( ) )" functionally equal in JavaScript? [duplicate]

Both of these code blocks below alert foo then bar. The only difference is })() and }()).

Code 1:

(function() {     bar = 'bar';     alert('foo'); })();  alert(bar); 

Code 2:

(function() {     bar = 'bar';     alert('foo'); }());  alert(bar); 

So is there any difference, apart from the syntax?

like image 531
Amy B Avatar asked May 09 '11 15:05

Amy B


People also ask

What doe => mean in JavaScript?

It's a new feature that introduced in ES6 and is called arrow function. The left part denotes the input of a function and the right part the output of that function.

How do you call a function within a function in JavaScript?

To call a function inside another function, define the inner function inside the outer function and invoke it. When using the function keyword, the function gets hoisted to the top of the scope and can be called from anywhere inside of the outer function.

How many functions are there in JavaScript?

There are 3 ways of writing a function in JavaScript: Function Declaration. Function Expression. Arrow Function.

What is an anonymous function in JavaScript?

Anonymous Function is a function that does not have any name associated with it. Normally we use the function keyword before the function name to define a function in JavaScript, however, in anonymous functions in JavaScript, we use only the function keyword without the function name.


2 Answers

No; they are identical


However, if you add new beforehand and .something afterwards, they will be different.

Code 1

new (function() {     this.prop = 4; }) ().prop; 

This code creates a new instance of this function's class, then gets the prop property of the new instance.
It returns 4.

It's equivalent to

function MyClass() {     this.prop = 4; } new MyClass().prop; 

Code 2

new ( function() {     return { Class: function() { } };  }() ).Class; 

This code calls new on the Class property.
Since the parentheses for the function call are inside the outer set of parentheses, they aren't picked up by the new expression, and instead call the function normally, returning its return value.
The new expression parses up to the .Class and instantiates that. (the parentheses after new are optional)

It's equivalent to

var namespace = { Class: function() { } };  function getNamespace() { return namespace; }  new ( getNamespace() ).Class; //Or, new namespace.Class; 

Without the parentheses around the call to getNamespace(), this would be parsed as (new getNamespace()).Class — it would call instantiate the getNamespace class and return the Class property of the new instance.

like image 109
SLaks Avatar answered Sep 22 '22 23:09

SLaks


There's no difference - the opening brace only serves as a syntactic hint to tell the parser that what follows is a function expression instead of a function declaration.

like image 45
Alnitak Avatar answered Sep 23 '22 23:09

Alnitak