嵌入函数问题

发布于 2024-10-08 14:27:00 字数 421 浏览 5 评论 0原文

例如:

//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()

我的问题是,f1和f2是否指向内存中的相同代码,为什么它们接缝不同的函数?每次调用 a 时,函数 a 是否都会花费时间来创建嵌入函数?

而且GC的工作方式也很有趣,a执行完后,a的局部变量不会被GC,而必须是返回的embed函数被GC后才被GC,因为返回的embed函数仍然可以调用函数a的局部变量。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦与时光遇 2024-10-15 14:27:00

回答你的问题:

  1. 不,它们指向不同的函数
  2. 当然,创建新函数需要一点时间
  3. 这是因为返回的函数是闭包,并且仍然可以引用它们创建的范围。

函数之间的差异语句和表达式

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
}

闭包

function test() {
    var b = 123;

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

有关闭包的更多信息,请参阅此问题 和答案。

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.

等风来 2024-10-15 14:27:00

我的问题是,f1和f2是否指向内存中的相同代码,为什么它们接缝不同的函数?

它们并不指向同一个函数。

每次调用 a 时,函数 a 是否都会花费时间来创建嵌入函数?

是的,每次调用 a 函数时,都会创建并返回一个新的内部函数。

而且GC的工作方式也很有趣,a的局部变量在a执行后不会被GC,而必须是返回的嵌入函数GC后才被GC,因为返回的嵌入函数仍然可以调用函数a的局部变量。

是的,这就是所谓的闭包。内部函数可以访问创建它的执行环境的所有变量。

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.

﹏半生如梦愿梦如真 2024-10-15 14:27:00

正如您所提到的,嵌入式函数可以访问局部变量。这称为闭包

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文