0

As soon as the function is declared wth function keyword javascript assigns a block of memory to the function name where function itself gets stored.

function maiz(){}
console.log(maiz);//output:function maiz(){}

but what will js do when function is declared anonymous or where will the anonymous function gets stored

(function (){})()

As soon as function is declared there should be some memory to store even the annonymos function and than execute it.Am i wrong?

Maizere Pathak.Nepal
  • 2,235
  • 3
  • 21
  • 38
  • but why do you want to know this? anyway, it depends on JS engine... – IProblemFactory Apr 27 '13 at 15:56
  • @ProblemFactory it sometime becomes compulsory to know – Maizere Pathak.Nepal Apr 27 '13 at 15:57
  • @Maizere Name an example of "sometime". – millimoose Apr 27 '13 at 16:00
  • @millimoose no example ,it was just my headache and i wanted to know about it ,that's it – Maizere Pathak.Nepal Apr 27 '13 at 16:06
  • @Maizere It's an implementation detail. One would hope the GC cleans it up sometime, but there's no way to know short of poring over the VM implementation. What happens to what memory depends on what exactly a "function object" is. I.e. a "function" object can have a reference to a "code" object that's shared between every instance of the same lexical "function", and it would actually be a bad idea to GC and recreate the code object repeatedly. (The way e.g. Python works, where such internals are exposed.) – millimoose Apr 27 '13 at 16:36

2 Answers2

2

You cannot declare an anonymous function. What you can do is have an anonymous function expression, which means you feed the function object somewhere (assignment, function call argument, etc). See Kangax' article or this question for the difference.

So if you want to know where an anonymous function expression goes to (in the memory), you will have to look at the surrounding statements. This one for example:

 (function (){});

would immediately after it has been instantiated be vanished by the garbage collector. And if you have

 (function (){})();

then the code inside will be executed (in a new scope), but the function itself will not get stored anywhere as well. Btw, this construct is called an immediately-invoked function expression (IIFE).

Community
  • 1
  • 1
Bergi
  • 513,640
  • 108
  • 821
  • 1,164
  • u missed executed,it should be" would immediately get executed" in the third line – Maizere Pathak.Nepal Apr 27 '13 at 16:08
  • @Maizere: No, in my first snippet there is no invocation. – Bergi Apr 27 '13 at 16:12
  • @Bergi If the function is not stored anywhere, how does the execution context gets the scope? – Suraj Jain Jan 05 '20 at 15:21
  • @SurajJain The function object is not used for anything else but creating the new execution context, it only exists temporarily. It might reside in a register, not in memory, or it might be optimised away completely, but my main point was that it is not stored in js variable accessible from user code. – Bergi Jan 05 '20 at 21:59
0

Anonymous functions are better explained in the book Secrets of the JavaScript Ninja(John Resig)

We can declare an anonymous function as a property of an object.

var ninja = {
    shout: function(){ // shout property now referenced to anonymous function 
        assert(true,"Ninja");
    }
};

Anonymous functions are typically used in cases where we wish to create a function for later use, such as storing it in a variable, establishing it as a method of an object, or using it as a callback (for example, as a timeout or event handler). In all of these situations, the function doesn’t need to have a name for later reference.

If there’s no need for a function to be referenced by its name, we don’t have to give it one (Anonymous function). It behaves as actual function which has name. But it doesn't have name. So Anonymous functions are stored where javascript functions are stored.

Ikrom
  • 3,635
  • 4
  • 42
  • 68