Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do empty parentheses () after a function declaration do in javascript? [duplicate]

Tags:

javascript

I'm trying to read the Prototype source. I've come to this part.(Unfortunately, this snippet is in the beginnning).

What does this () mean?

  Browser: (function(){     var ua = navigator.userAgent;     var isOpera = Object.prototype.toString.call(window.opera) == '[object Opera]';     return {       IE:             !!window.attachEvent && !isOpera,       Opera:          isOpera,       WebKit:         ua.indexOf('AppleWebKit/') > -1,       Gecko:          ua.indexOf('Gecko') > -1 && ua.indexOf('KHTML') === -1,       MobileSafari:   /Apple.*Mobile.*Safari/.test(ua)     }   })(), 

I am referring to the last line before the comma?

like image 318
Teej Avatar asked Mar 11 '10 01:03

Teej


People also ask

What do empty parentheses mean in JavaScript?

The code is defining an anonymous function (the (function (){ ... }) bit) and then calling it (with no arguments). It then assigns the value to the Browser property of the object that is presumably being defined outside of your code snippet.

What is the significance of empty parentheses in a function declaration?

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

What goes in the parentheses after a function JavaScript?

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.

Why are parenthesis used to wrap a JavaScript function call?

The parentheses encapsulating the function declaration tell the JavaScript engine to execute the code immediately after it's parsed.


2 Answers

The code is defining an anonymous function (the (function (){ ... }) bit) and then calling it (with no arguments). It then assigns the value to the Browser property of the object that is presumably being defined outside of your code snippet.

You could also define the function somewhere:

function myFunction() {     var ua = navigator.userAgent;     var isOpera = Object.prototype.toString.call(window.opera) == '[object Opera]';     return {       IE:             !!window.attachEvent && !isOpera,       Opera:          isOpera,       WebKit:         ua.indexOf('AppleWebKit/') > -1,       Gecko:          ua.indexOf('Gecko') > -1 && ua.indexOf('KHTML') === -1,       MobileSafari:   /Apple.*Mobile.*Safari/.test(ua) } 

and then call it:

var foo = myFunction(); 

and then assign the value:

... Browser: foo, ... 

One downside with doing it that way is that you "pollute your namespace" with a function and a variable that you won't use anywhere else. The second issue is that you can't use the value of any locally-scoped variables in your function definition (the anonymous function behaves as a closure).

like image 130
Frank Schmitt Avatar answered Sep 20 '22 22:09

Frank Schmitt


(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. So, you end up with boolean values for, e.g., Browser.IE, Browser.Opera, etc.

like image 23
Andrew Hedges Avatar answered Sep 20 '22 22:09

Andrew Hedges