Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

有关“闭包”、“执行上下文”的不同见解。 #48

Open
crayoncloud opened this issue Mar 3, 2023 · 0 comments
Open

有关“闭包”、“执行上下文”的不同见解。 #48

crayoncloud opened this issue Mar 3, 2023 · 0 comments

Comments

@crayoncloud
Copy link

crayoncloud commented Mar 3, 2023

1、闭包
在您的文章中,闭包被描述在堆内存中,从内部函数EC的作用域链中,把用到的外层变量拷贝过来的一个对象。但是在我理解中,闭包应该是一个对作用域链的复用。即原本在调用栈的内部函数执行完被推出栈之后,内存回收会回收掉这个出栈的函数EC。但是因为该内部函数被返回到了外部储存了下来,所以这个外部储存的变量产生了对内部函数作用域链的引用(详细机制是通过函数定义时,私有属性保存的作用域),从而导致该被回收的没有被回收。
这个机制跟您说的那个机制仔细看是不同的,你说的那个存在一个拷贝值的过程,我说的这个,是对现有数据的复用。

2、在讲解v8如何运行js时,您有一张图描述了执行上下文是在生成AST之后,在解释之前生成的。因此,并没有具体讲解EC到底是Parser生成的,还是解释器的编译器生成的。
在我的理解中,Parser只用来生成AST,进入到解释器,就是代码运行阶段,会先编译生成EC和可执行代码(机器码),然后进入执行阶段,即机器码在EC中,被推入调用栈执行。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant