简体   繁体   English

完整代码或嵌套功能级别是否一次进行提升

[英]Does hoisting takes place at once for the full code or by nested-function-levels

Hy guys. 大家好 I dont understand something regarding the hoisting and it can be it is my bad, but I didnt find any answer, neather here nor on google, thatswhy I ask, thanks for reading. 我不了解有关起重装置的某些信息,可能是我的问题,但我没有找到任何答案,无论是在这里还是在Google上,这都是我想问的,感谢您的阅读。

So I dont understand, As the javascript engine gets my code below and starts to scan through, will be the whole code with all functions and nested functions scanned threw, until to the very last scope? 所以我不明白,随着javascript引擎在下面获取我的代码并开始进行扫描,是否会将包含所有函数和嵌套函数的整个代码扔到最后一个范围? And the creation phase of all function will take place for the first scan (or with others words will the full code be scanned just once and prepared everything for each functions)? 并且所有功能的创建阶段都将在第一次扫描时进行(或者换句话说,整个代码将只被扫描一次并为每个功能准备好一切)?

 /* Global execution context*/ function myFirst(){ /*Scanner meets this code and hoists it*/ var A = "12" return function myFirstB(){ /*As the scanner arrived here and scanns threw this function during the parents hoisting period ( or just before the global contexts execution phase) will it be hoisted as well, that it gets [[Scopes]] property? So that when I call it in the last line as a closure, it can remember on, that variable "A" is in its outer-environment?*/ console.log(A) } //myFirstB() } // myFirst() function mySecond(){ alert("one") } var myClosure = myFirst(); myClosure(); /*before beeing called does this function have already [[Scopes]] property/scope-chain?*/ 

Or the hoisting happens nest-level by nest-level? 还是吊运是逐层进行的? So I mean at first those functions will be hoisted which are defined in the global context? 因此,我的意思是说,首先要提升那些在全局范围内定义的功能? and then as one of those functions gets invoked and its execution-contexts execution phase starts, will be its nested functions hoisted? 然后,当其中一个函数被调用并且其执行上下文的执行阶段开始时,将嵌套其嵌套函数吗?

I am investigating this, because I dont really udnerstand, how a nested function remembers on, in which lexical environment/function has it been defined, if it wasnt already at least hoisted, that it has a [[Scopes]] property, which preserves its scope chain 我正在对此进行调查,因为我不太了解嵌套函数是如何记住的,如果至少尚未举起,则在其中定义了词汇环境/函数(如果至少尚未提升),它具有[[Scopes]]属性,该属性可以保留其范围链

The probelm is all the articles I saw until now and even the ecmascript 6 documentation says only, that the hoisting happens, if the scanner meets a function definitiona and then the scope property will be created with the scope chain and variable object the arguments object and the "this" keyword, but I didnt find any material which would talk about, if the nested functions (which are preserved in the variable object, coupled there with a reference to their function body in the memory) will be at least hoisted as well (at the same time, their parent function is hoisted) and soo they get a scope chain to remember on their outer environment, if they are called outside from there outer-environment 该问题是直到现在为止我所看到的所有文章,甚至ecmascript 6文档也只说过,如果扫描程序满足功能定义a,然后使用范围链和变量对象,参数对象和“ this”关键字,但是,如果嵌套的函数(保留在变量对象中,并在内存中引用了它们的函数体)也不会被提升,那么我没有找到任何可以讨论的材料(同时,其父函数已被吊起),因此,如果从那里的外部环境中调用它们,它们将获得一个范围链以记住其外部环境

Thanks a lot guys for reading threw my tonns of words, if you can answer it or if you have an article which talks about this aspect as well of hoisting, I would really appriciate 非常感谢许多人阅读我投入的大量文字,如果您能回答,或者您有一篇文章谈论这方面以及吊装,我真的很高兴

I think you are confused because you tangled too many things. 我认为您很困惑,因为您纠结了太多东西。 You will need to distinguish three things: 您将需要区分三件事:

  • what the parser does 解析器做什么
  • what happens when a scope is created 创建范围时会发生什么
  • what happens when code is run 运行代码时会发生什么

The parser does indeed scan through the whole code. 解析器确实确实扫描了整个代码。 If there was a syntax error anywhere, none of the code would be run. 如果任何地方都存在语法错误,则不会运行任何代码。 It does parse the whole code into a (nested) structure that is suitable for execution later. 它确实将整个代码解析为一个(嵌套的)结构,该结构适合以后执行。 This might be an abstract syntax tree or executable byte code, or anything in between - that's an implementation detail. 这可能是抽象的语法树或可执行的字节码,或两者之间的任何内容-这是实现细节。

Before a chunk of code is run, like in the global scope or a block scope or a function scope, the context for that execution has to be created and initialised first. 在运行代码块之前,例如在全局作用域,块作用域或函数作用域中,必须先创建和初始化该执行的上下文。 It does get a parent scope reference (eg the scope surrounding a block or the scope a closure was defined in), a this value, and a new variable environment in which the variables of this scope are created. 它确实获得了父作用域引用(例如,围绕块的作用域或在其中定义了闭合的作用域),一个this值以及一个在其中创建该作用域的变量的新变量环境。 Here is where all the variable names declared in the scope (eg function parameters, var and function and let and const and class declarations etc) are used to instantiate a new variable. 这里是在范围内声明的所有变量名(例如,函数参数, varfunction以及letconstclass声明等)用于实例化新变量的地方。

When the code chunk is executed statement for statement and expression for expression, the variables already exist. 当执行代码块时,对于语句的语句和对于表达式的表达式,变量已经存在。 This is when the values are created and assigned to the variables. 这是创建值并将其分配给变量的时间。 When a function is defined, it does become a closure by referencing the current scope. 定义一个函数时,通过引用当前作用域,它确实成为一个闭包。

The term "hoisting" is giving the wrong impression . 术语“吊装” 给人以错误的印象 No source code is moved around. 不会移动任何源代码。 How an implementation achieves the specified behaviour is not restricted. 实现方式如何实现指定的行为不受限制。 It might be that the parser creates byte code that has the creation of the variables at the top of each scope. 解析器可能创建了字节代码,该字节代码在每个作用域的顶部都有变量的创建。 It might be that in the initialisation phase of a scope it scans the AST for declarations every time. 可能是在范围的初始化阶段,它每次都扫描AST以查找声明。 It most likely is a mix of those in the form of a just-in-time compiler, which creates the byte code only when the function is called for the first time. 它很可能是即时编译器形式的混合形式,仅在首次调用该函数时才创建字节码。

Regarding your question about nested functions, remember that the inner function is re-created every time the outer function is called. 关于嵌套函数的问题,请记住,每次调用外部函数时都会重新创建内部函数。 It is only "hoisted" to the top of its scope, not outside of the outer function, and will be redefined on every run of the code. 它仅“提升”到其作用域的顶部,而不在外部函数之外,并且将在每次运行代码时重新定义。

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

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