函数是否在javascript'创建阶段'中设置变量之前? [英] Are functions set before variables in the javascript 'creation phase'?

查看:66
本文介绍了函数是否在javascript'创建阶段'中设置变量之前?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行Udemy课程 Javascript:了解奇怪的部分,我刚刚了解了解释器解释JS时发生的创建阶段和执行阶段。

I am doing the Udemy course Javascript: Understanding the Weird Parts right now, and I just learned about the creation phase and the execution phase that occurs when the interpreter interprets the JS.

我有一个问题,但我会先向您展示代码I我正在玩:

I have a question, but I will first show you the code I am playing with:

http:// codepen。 io / rsf / pen / bEgpNY

b();

function b () {

  console.log(a);
}

var a = 'peas';

b();

如果我理解正确,在创建阶段,变量和函数是'set',这意味着它们在记忆中有斑点。这些变量都给出了undefined的占位符值。然后在执行阶段,引擎执行从顶部开始的行。当第一次调用b()时,'a'的占位符值仍为undefined,则'a'的初始值为'豌豆',b()再次被调用,此时'a'的值为'豌豆'。
在我看来,这里必须发生两件事之一。备选方案1:在创建阶段,所有变量都在函数之前设置。这意味着当创建函数b()的内存空间时,该函数包含未定义的值(因为'a'内存空间已经使用'undefined'的值创建)。备选方案2:函数和变量按照它们所处的词汇顺序设置(在这种情况下,b在a之前创建),当创建b时,'a'引用以某种方式表示函数正在侦听任何可能创建的'a'内存位置,当稍后实际创建'a'位置时,引用会引用该位置。

If I understand correctly, in the creation phase, the variables and functions are 'set', meaning they are given spots in memory. The variables are all given the placeholder value of undefined. Then in the execution phase, the engine executes the lines starting at the top. When b() is first called, 'a' still has the placeholder value of undefined, then 'a' is given its initial value of 'peas', b() is called again and this time 'a' has the value of 'peas'. In my mind, one of two things has to be happening here. Alternative 1: In the creation phase, all variables are set before functions. This means that when a memory space for the function b() is created, the function includes a's value of undefined (because the 'a' memory space was already created with the value of 'undefined'). Alternative 2: the functions and variables are set in the lexical order they are in (in this case, b is created before a), and when b is created, the 'a' reference somehow means that the function is listening for any possible creation of an 'a' memory location, and when later the 'a' location is actually created, the reference refers to that spot.

Am我在这两种情况下的正确轨道上?

Am I on the right track with either of these scenarios?

推荐答案


如果我理解正确的话,在创建阶段,变量和函数是'set',这意味着它们在内存中被赋予了斑点。

If I understand correctly, in the creation phase, the variables and functions are 'set', meaning they are given spots in memory.

我不会为此使用术语 set - 它通常用于引用变量被设置为(已分配)特定值。我也不会使用术语点或记忆 - 我们不需要担心这些内部。只是说声明更清楚。

I would not use the term set for this--it usually is used to refer to a variable being set to (assigned) a particular value. I also would not use the term "spot" or "memory"--we don't need to worry about these internals. It's clearer just to say declared.

我也不太喜欢使用术语创建阶段,这两者都是非标准和混乱 - 究竟是什么创造的?我更喜欢术语编译。

I also don't really like the use of the term "creation phase", which is both non-standard and confusing--what is being created, exactly? I would prefer the term "compilation".


所有变量都给出了未定义的占位符值。

The variables are all given the placeholder value of undefined.

准确地说,我不会说它们具有未定义的值,而是没有价值或未定义。 undefined 不是尚未分配给的变量所持有的值;而且它是一个状态,这会导致变量在访问时计算为 undefined 值。

To be precise, I would not say they "have the value of undefined", but rather "have no value", or "are not defined". undefined is not a value held by a variable which has not been assigned to yet; rather it's a state, which causes the variable to evaluate to the undefined value when accessed.


备选1:在创建阶段,所有变量都在函数之前设置。

Alternative 1: In the creation phase, all variables are set before functions.

是的,虽然再次使用set这个词会让人感到困惑。比如说,所有变量都是在函数之前声明。这是提升的过程。

Yes, although again it's going to be confusing to use the word "set". Say, "all variables are declared before functions". This is the process of hoisting.


备选方案2:函数和变量按词法顺序设置在(在这种情况下,b在a之前创建),并且当创建b时,'a'引用以某种方式表示该函数正在侦听任何可能的'a'存储位置的创建,并且当稍后'a'时'位置实际上是创建的,引用是指该位置。

Alternative 2: the functions and variables are set in the lexical order they are in (in this case, b is created before a), and when b is created, the 'a' reference somehow means that the function is listening for any possible creation of an 'a' memory location, and when later the 'a' location is actually created, the reference refers to that spot.

否。该功能不会听任何东西。它会在你告诉它时执行。

No. The function does not "listen" to anything. It just executes when you tell it to.

不是真的。它属于奥秘的范畴。所以我们用这样的规则阻塞我们的大脑,变量以这种方式提升,函数声明以其他方式提升,还有其他一些提升行为。在实践中,几乎所有样式指南都会要求您在函数顶部声明变量,如果您没有(或者可以配置这样做),则linters会发出警告。这立即消除了所有可变的提升问题。

Not really. It falls into the category of arcana. So we clog up our brains with rules like, variables hoist this way, function declarations hoist some other way, let has yet some other hoisting behavior. In practice, almost all style guides will call for you to declare variables at the top of the function, and linters will warn you if you don't (or can be configured to do so). This immediately eliminates all variable hoisting issues.

有些人喜欢将内部函数放在函数的底部,这样可行,因为它是函数声明(即函数foo) (){} )整个事物(包括定义)被悬挂。如果它是一个赋值给变量的函数表达式(即 var foo = function(){} ),那么它就是一个变量,我们已经决定将它们放在最顶层我们的功能 - 见上面的段落。

Some people like to put internal functions at the bottom of their function, and that works fine since, if it's a function declaration (ie function foo() { }) the whole thing (including the definition) is hoisted. If it's a function expression being assigned to a variable (ie var foo = function() { }), then it's a variable, and we already decided to put those at the top of our function--see paragraph above.

一般来说,如果你的程序依赖于提升行为,那就写得很糟糕。如果你需要了解提升行为以理解程序是如何工作的,那就写得很糟糕。

In general, if your program depends on hoisting behavior, it's written badly. If you need to understand hoisting behavior to understand how the program works, it's written badly.

总而言之,你真正需要学习的是一条规则:放置变量声明(和它们的初始化)在你的函数的顶部。然后你根本不用担心吊装。

To summarize, all you really need to learn is one rule: put variable declarations (and their initializations) at the top of your function. Then you don't have to worry about hoisting at all.

(有一些例外,例如在声明语句中的变量,如 for(var i ...),这很好,假设 i 没有用于循环索引以外的任何其他内容。)

(There are some exceptions, such as declaring a variable inside a for statement, as in for (var i...), which is fine, assuming i is not being used for anything other than the index of the loop.)

出于某种原因,学习JS的人似乎有时会专注于这些奇怪的事情 - 例如为什么== false 或其他什么。我建议改为专注于如何思考你的问题,打破它们,编写好的干净代码才能正常工作,你和其他人可以保持而不用担心奥秘我多年来一直在写JS,不记得上次遇到与吊装有关的问题。

For some reason, people learning JS seem sometimes to focus on these oddities--such as "why does " " == false or something. I would suggest instead focusing on the how to think about your problems, and break them down, and writing nice clean code that just works, and that you and other people can maintain without worrying about the arcana. I've been writing JS for many years, and cannot remember the last time I encountered a problem related to hoisting.

这篇关于函数是否在javascript'创建阶段'中设置变量之前?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆