0

for example:

//global vars
var g1,g2,g3;
function a(p1,p2,p3){
  //local vars
  var a1,a2,a3;
  return function(){
    //an embed function, looks like dynamic defined at runtime
  }
}

var f1 = a(1,2,3)
var f2 = a(4,5,6)
f1()
f2()

my question is , is f1 and f2 point to the same code in memory, why they seams diffrent function? does function a spend time to create the embed function when a is call each time?

and GC works also very intresting, local vars of a will not be GC after a executed, It must be GC after the returned embed function GCed, cause of returned embed function still can invoke the local vars of function a.

Ivo Wetzel
  • 46,459
  • 16
  • 98
  • 112
guilin 桂林
  • 17,050
  • 29
  • 92
  • 146

3 Answers3

1

To answer your questions:

  1. No they point to different functions
  2. Of course it take a little time to create the new functions
  3. That is because the returned functions are closures and can still reference the scope in which they were created in.

Difference between Function Statement and Expression

function foo() {} // created at compile time, is available every in the code from the start

var foo = function() {}; // created at runtime, only is available after it's creation
                         // while foo already exists it's set to undefined

function test() {
    return function(){}; // returns a new function each time test is called
}

Closures

function test() {
    var b = 123;

    // this function inherits all outer scopes so it still has access to b
    return function() {
        console.log(b); //
    };
}

For more on closures see this question and the answers.

Community
  • 1
  • 1
Ivo Wetzel
  • 46,459
  • 16
  • 98
  • 112
  • There is no compiling in JavaScript. Function declarations are also evaluated at runtime. The difference is that they are evaluated before any other statements in the current execution context. – Šime Vidas Dec 18 '10 at 17:34
  • @Sime no compiling in javascript, reasonable, but how to explain f1 and f2 is diffrent function. – guilin 桂林 Dec 18 '10 at 17:53
  • @Sime yea, I refer to that as compile time most of the time ;) @guilin because the `function()` expression creates a new function each time it's called. – Ivo Wetzel Dec 18 '10 at 17:58
1

my question is , is f1 and f2 point to the same code in memory, why they seams diffrent function?

They don't point to the same function.

does function a spend time to create the embed function when a is call each time?

Yes, every time you call the a function, a new inner function will be created and returned.

and GC works also very intresting, local vars of a will not be GC after a executed, It must be GC after the returned embed function GCed, cause of returned embed function still can invoke the local vars of function a.

Yes, that's called closure. The inner function has access to all variables of the execution environment in which it was created in.

Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
0

As you mentioned, embedded functions have access to the local variables. This is called a closure.

Brian
  • 37,399
  • 24
  • 94
  • 109