user3466034
user3466034

Reputation: 107

Javascript Function Definition Explanation

Can anyone explain me the following function inside a JS file

function Init()
{
  (function set()
     {
       -----code---
       print "hello";
      }
   )();
}

If I call the function Init , does it automatically run the function set as well or i need to call set() to run it?

Upvotes: 3

Views: 115

Answers (4)

Dominique Fortin
Dominique Fortin

Reputation: 2238

When you do

(function set() { /*code here*/ })();

, it is a close equivalent to

var set = function () { /*code here*/ };
set();

So when you call Init(), the set function will be called.

Upvotes: 0

Zlatko
Zlatko

Reputation: 19569

You need to call Init to run any code inside of it. Now, that code inside is just regular JavaScript code, but an interesting one. Let's see why.


How

Your inner function is a so-called IIFE. What you can do to make it easier to read the outer function (Init), is to replace the inner function with the result of the IIFE call.

Here are a few examples:

var x = undefined; // x === undefined
x = (undefined); // again
x = 3;      // x === 3
x = (3);   // again the same
x = 'some string'; // some string
x = ('some string'); // the same

So it's ok to put () around an object, you get the same object. The same for a function

x = function() {};   // x is that function now.
x = (function() {}); // the same.

But if you say x = 3;, can you call x?

x = 3;
x(); // syntax error
x = (3);
x(); // again error. can't call a number.

But, if your x is a function, you can call it:

x = function() { console.log('Hi'); }; // you can call x now.
x(); // logs 'Hi'!

So if you do the same with parenthesis, it's the same:

x = (function() { console.log('Hi'); }); // again, the same.
x(); // no error, function called again!

So you can skip the assignment part:

// instead of x = (...); and the x(), you replace the x part right away.

(function() { console.log('Hi'); })()
// function body       up to here ^^ now the call part.

Note that it won't work without the first pair of parens:

function() { console.log('Hi'); }(); // syntax error.

So you're putting the outer parens () just to cast that function expression into an object in-place.


Why

So that's how it works. But why? Because you wanna make sure nobody else calls your function! If you do this:

var x = function() {};
x();

Now there are at least two things that can happen that you might not want:

  1. There might be a name clash. You might not know if there's an "x" already defined on wherever the outer function is gonna be called at, e.g. you bind Init to some object which has x at scope or something.

  2. Somebody can "steal" your x and call it again. Not necessarily steal, you just leak it (a subtle bug) and some other code calls x, expecting to find its own x, but in fact, it's your x now because it is reachable. And you wanted it not to be.

So you handily lock it away in anonymous function that's executed right away.

Hope this clears things a bit.

Upvotes: 1

ymz
ymz

Reputation: 6914

this is a basic function closure in js (an inner function inside another one).

also.. it is anonymous function..meaning...you can only use that function once (and that is why the function call is attached to function deceleration). so...

function regular()
{
    function regular_closure()
    {

    }

    (function () // this is anonymous
    {

    })(); // call the anonymous function.. which is also a closure

    regular_closure(); // call the function
}

more about this topic:

MDN - js closures in depth

When to use js closure

Upvotes: 0

Darren
Darren

Reputation: 70718

You would need to call Init(); to execute the function.

The (function () {console.log('IIFE') }());

Syntax you are referring to is an immediately-invoked function expression.

Upvotes: 0

Related Questions