简体   繁体   English

Javascript - 上下文执行阶段

[英]Javascript - Context Execution phases

I know that the "execution" in JavaScript happens in 2 phases:我知道 JavaScript 中的“执行”分为两个阶段:

1) The Creation phase when the functions and variables are added in the memory, hoisting,the this is create and outer environment 1)Creation阶段,在内存中添加函数和变量,提升,这是创建和外部环境

2) The second phases when the code is executed. 2) 代码执行的第二阶段。

The question: The variables and functions insides a parent function are added in the memory when the script start or only when the parent function is invoked ?问题:父函数内部的变量和函数是在脚本启动时还是仅在调用父函数时添加到内存中?

There is any difference between behavior of parameters of a function and variables declared inside function, regarding this aspect ?在这方面,函数参数的行为与函数内部声明的变量的行为有什么区别? (I ask this because in other languages they behave different - the parameters are in memory before function executes, but in the scope of function) (我问这个是因为在其他语言中它们的行为不同 - 参数在函数执行之前在内存中,但在函数范围内)

If the variables and functions inside all functions were been put into the memory when the script starts, you memory may be full at start.如果脚本启动时所有函数中的变量和函数都被放入了内存中,那么你的内存可能在启动时已满。 When a function executes, at that time the variables and inside functions are added into the memory.当一个函数执行时,此时变量和内部函数被添加到内存中。 When the function ends its execution, the related variables are removed from the memory, if they are not used in closure .当函数结束执行时,相关变量将从内存中删除,如果它们没有在闭包中使用

There is any difference between behavior of parameters of a function and variables declared inside function函数参数的行为与函数内部声明的变量之间存在任何差异

There is no obvious deference in the behavior, the parameters are also variables which are declared in the scope of the function, only they can have their values from the outer.行为上没有明显的尊重,参数也是在函数范围内声明的变量,只有它们可以从外部获得它们的值。

One difference can be when you passed a reference type to the function and change its property in the functions.一个区别可能是当您将引用类型传递给函数并在函数中更改其属性时。 So it will be changed outside the function itself.所以它会在函数本身之外被改变。 This means that parameters can be bounded to the outer world in some manners.这意味着参数可以以某种方式绑定到外部世界 But I think this is not related to the behavior of parameters and scoped variables.但我认为这与参数和作用域变量的行为无关。 They both are scoped into the function.它们都被限定在函数中。

the parameters are in memory before function executes, but in the scope of function参数在函数执行前在内存中,但在函数范围内

Javascript functions can take varied number of parameters. Javascript 函数可以采用不同数量的参数。 So it can't add the parameters to the memory.所以它不能将参数添加到内存中。 You can have a function with no parameters, but call it and pass 10 parameters to it.你可以有一个没有参数的函数,但是调用它并向它传递 10 个参数。

You are essentially asking things that are dependent on the inner workings of the JS engine.您本质上是在询问依赖于 JS 引擎内部工作的事情。 A JavaScript program can go through many phases, including a "parsing" phase, but in practice there are different kinds of parsing occurring in multiple phases, at different levels, for different purposes. JavaScript 程序可以经历许多阶段,包括“解析”阶段,但实际上,出于不同目的,在多个阶段、不同级别上会发生不同类型的解析。 (I would not call this the "Creation" phase; that term is non-standard and confusing.) The result of various parsing stages can be an internal syntax tree, or an intermediate byte-code representation, or even machine code ready to be executed. (我不会称其为“创建”阶段;该术语是非标准且令人困惑的。)各种解析阶段的结果可以是内部语法树,或中间字节码表示,甚至是准备好的机器代码执行。 The same code might be parsed in different ways at different points in time, or in some cases parsed and then the result thrown away and reparsed again later, etc.相同的代码可能会在不同的时间点以不同的方式被解析,或者在某些情况下被解析然后结果被丢弃并稍后再次重新解析,等等。

The whole point is that, unless you are learning compiler theory, it doesn't matter.重点是,除非您正在学习编译器理论,否则没关系。 Yes, parsing can be considered to be where "hoisting" occurs.是的,解析可以被认为是“提升”发生的地方。 However, I would urge you to not spend too much time worrying about hoisting, unless you plan to enter some kind of JS trivia contest.但是,我劝你不要花太多时间担心提升,除非你打算参加某种 JS 琐事竞赛。 Just declare all your variables (including variables to which you assign functions) at the top of each function, as you should have been doing all along, then stop worrying about it.只需在每个函数的顶部声明所有变量(包括您为其分配函数的变量),就像您一直以来应该做的那样,然后就不必担心了。 While the other guys are patting themselves on the back for knowing whether or not ES6 class es are hoisted, you can be actually writing useful code.当其他人拍拍自己的背以了解 ES6 class是否被提升时,您实际上可以编写有用的代码。

The question of when and how and where memory is allocated is also an internal engine detail.何时、如何以及在何处分配内存的问题也是一个内部引擎细节。 Some items might be allocated on the "heap" (of which there might be more than one), and then garbage-collected later.某些项目可能会在“堆”(其中可能有多个)上分配,然后在稍后进行垃圾收集。 Such allocation would almost always happen at run-time, but this is also not a hard-and-fast rule;这种分配几乎总是在运行时发生,但这也不是硬性规定; for example, a regexp encountered identified during parsing might be cached in compiled form on the heap.例如,在解析过程中遇到的正则表达式可能会以编译形式缓存在堆上。 Other variables, such as primitives, are likely to be allocated on the "stack", and de-allocated semi-automatically when the stack frame is released when returning from a function.其他变量,例如原语,可能会在“堆栈”上分配,并在从函数返回时释放堆栈帧时半自动地解除分配。 Variables which need to be maintained for use in embedded functions ("closures") are allocated and stored in a particular way.需要维护以用于嵌入函数(“闭包”)的变量以特定方式分配和存储。

I doubt if knowing any of this is going to make you a better JavaScript programmer in any meaningful way.我怀疑了解这些是否会让你以任何有意义的方式成为更好的 JavaScript 程序员。 Learning how one engine does it is not going to necessarily help you understand how another engine does it, or even how that same engine is going to do it in the next version.了解一个引擎是如何工作的,并不一定能帮助您了解另一个引擎是如何工作的,甚至在下一个版本中该引擎将如何工作。

Do you have a specific reason for wanting to know this, other than curiosity, or a specific case where you think it would make a difference?除了好奇心或您认为会有所作为的特定案例之外,您是否有想知道这一点的特定原因?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM