声明与初始化变量? [英] Declaring vs Initializing a variable?

查看:153
本文介绍了声明与初始化变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想知道声明变量和初始化变量之间的区别。例如。

I'm curious to know the difference between declaring a variable, and initializing a variable. e.g.

var example; // this is declaring

var example = "hi" // initializing? Or just "adding a value"?

我不认为我就在那里,但每个人的定义究竟是什么?或者它们基本上是一样的意思吗?

I don't think I'm right there, but what exactly is the definition of each? Or do they basically mean the same thing?

推荐答案

编辑:@ThisClark在评论中说了些什么,我去了为了证明他是错的,在阅读规范之后,我学到了更多东西:

除了规范


var 语句声明了作用于正在运行的执行上下文的VariableEnvironment的变量。 Var变量在实例化包含词法环境时创建,并在创建时初始化为 undefined 。 [...]在执行VariableDeclaration时,由VariableDeclaration定义的具有初始化程序的变量被赋予其初始化程序的AssignmentExpression的值,而不是在创建变量时。

A var statement declares variables that are scoped to the running execution context’s VariableEnvironment. Var variables are created when their containing Lexical Environment is instantiated and are initialized to undefined when created. [...] A variable defined by a VariableDeclaration with an Initializer is assigned the value of its Initializer’s AssignmentExpression when the VariableDeclaration is executed, not when the variable is created.

根据我对此的解读,以下几点描述了您在问题中询问的行为(以及术语的正确用法):

Based on my reading of this, the following points describe the behavior (and "correct-ish" usage of the terms) you asked about in your question:


  • 变量声明(例如, var foo )会导致该变量为<一旦词法环境被实例化,em>创建。例如,如果该变量是在函数体中定义的,那么该函数就是词汇环境,因此变量创建与函数本身的实例化一致。

  • A variable declaration (e.g., var foo) causes that variable to be created as soon as the "lexical environment" is instantiated. For example, if that variable were defined within a function body, that function is the "lexical environment", and so variable creation coincides with the instantiation of the function itself.

显然,变量声明可能会或可能不会使用初始值设定项创建(即,解析为变量初始值的右侧表达式)。这是对初始值一词的非规范用法,但是......让我们再深入研究一下:

Obviously, variable declarations may or may not be created with an initializer (i.e., the right-hand expression that resolves to the variable's initial value). That's a pretty non-specification usage of the term "initial value", though... let's dig into that a bit more:

技术上,根据此处的规范说明,所有变量都使用值 undefined 初始化:

Technically, per the specification notes here, all variables are initialzed with the value undefined:


创建变量[...]并初始化为未定义

variables are created [...] and are initialized to undefined

强调技术上,因为这是如果还提供初始化程序,则主要是学术性的。

Emphasis on "technically" there, as this is largely academic if an initializer was also provided.

根据我们已经涵盖的内容,应该理解声明 var foo; 可以在函数体中 where 存在,并且将它移动到同一函数中的任何其他地方都不会对运行时语义产生影响。函数本身(无论是否发生任何实际作业或对 foo 的其他引用)。如果这仍然令人困惑,请重新阅读之前的观点。

Based on what we've already covered, it should be understood that the statement var foo; could exist anywhere within a function body, and moving it anywhere else within the same function would have no effect on the runtime semantics of the function itself (regardless of if/where any actual assignments or other references to foo take place). If that's still confusing, re-read the previous points.

最后一点行为是最直观的部分,那就是赋值初始化程序的em>。当这行代码实际上执行时,这种分配就会发生(同样,这个变量与技术上的创建的变量不同)。

The last bit of behavior is the most intuitive part, and that's the assignment of the initializer. This assignment takes place when that line of code is actually executed (which, again, isn't the same point in time when the variable technically got created).

所以,快速回顾一下:


  • 所有变量声明(使用 var 总是使用 undefined初始化 code>初始化他们的词汇环境。

  • 从技术意义上讲,这个初始化可能不算作赋值(ha,@这个克拉克,你 错了!!)。 :)

  • 分配是很容易的部分,因为它们的行为方式(以及在您预期的时间点)。

  • All variable declarations (that use var) are always initialized with undefined upon the initialization of their lexical environment.
  • This initialization probably doesn't count as an assignment, in the technical sense (ha, @ThisClark, you were wrong!!). :)
  • Assignments are the easy part, as they behave the way (and at the point in time) that you expect.

希望有所帮助(而且我并没有严重误解规范!)。

Hope that helps (and that I didn't terribly misinterpret the spec!).

这篇关于声明与初始化变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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