JavaScript 中的函数是否只在内存中保留一次?

问题描述

在问我的问题之前,让我给你一些信息。看这段代码

"use strict"

function outer() {

    let counter = 0;

    function incrementCounter() {   
        counter++;
        console.log(counter);
    }

    return incrementCounter;
}

const newFunction = outer();  // A 
newFunction();                // B
newFunction();                // C

这是一个闭包的例子。当A线完成时。全局内存看起来像这样:

                   Global     Memory
________________________________________________________________

                           
Function Name                                           Data

outer            ---------------------------->    { outer function code }
                                                        
                                                       
newFunction      ---------------------------->    { incrementCounter's code / definitions / functionalty }                                     
                                                  | live data attached to incrementCounter | 
                                                  | counter = 0 }                          |

实时数据部分包括计数器变量。

当我们在 B 行运行该函数时,当引擎首先遇到 counter++ 时,它会查找 new Function's 本地内存以查找计数器变量,如果没有找到,它会检查上面提到的实时数据。

当它最终在实时数据中找到计数器时,counter++ 起作用。对 C 行重复相同的事情。

最后,我们在控制台上看到了 1 和 2。

现在查看这段代码。

"use strict"

function outer() {

    let counter = 0;

    function incrementCounter() {   
        counter++;
        console.log(counter);
    }

    return incrementCounter;
}

const newFunction = outer();  // A 
newFunction();                // B
newFunction();                // C


const anotherFunction = outer(); // D 
anotherFunction();               // F
anotherFunction();               // G

当我们运行上面的代码时,我们会在控制台上看到 1 2 1 2。不是1 2 3 4。因为我知道 newFunction's 实时数据字段或闭合或背包与 anotherFunction's 不同。

这是我的问题:

1)在这种情况下全局内存是什么样的?

看起来像这样吗?

                   Global     Memory
________________________________________________________________

                           
Function Name                                           Data

outer            ---------------------------->    { outer function code }
                                                        
                                                       
newFunction      ---------------------------->    { incrementCounter's code / definitions / functionalty }                                     
                                                  | live data attached to incrementCounter | 
                                                  | counter = 2 }                          |

anotherFunction  ---------------------------->    { incrementCounter's code / definitions / functionalty }                                     
                                                  | live data attached to incrementCounter | 
                                                  | counter = 2 }                          |

或者看起来像这样?

                   Global     Memory
________________________________________________________________

                           
Function Name                                           Data

outer            ---------------------------->    { outer function code }
                                                        
                                                  |                Unique                  |
newFunction      ---------------------------->    | live data attached to incrementCounter | 
                 |                                | counter = 2 }                          |       
                 |                
                 | common function definition                 
                 |----------------------->        { incrementCounter's code / definitions / functionalty }  
                 |                                         
                 |                                |                Unique                  |    
anotherFunction  --------------------------->     | live data attached to incrementCounter | 
                                                  | counter = 2 }                          |  

2) 全局内存中是否只有一个或多个外部函数?

3)在全局内存中 incrementCounter 的函数定义是只有一个还是有两个函数定义?

更新

我想我应该在这里添加我的资源。我在观看 Codesmith 的 javascript 难点视频系列时学到了我上面提到的内容。

如果你能查一下this video,也许你能更好地理解这个问题。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)