未定义的变量 [英] undefined variables

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

问题描述




寻找未定义变量与错误的澄清

JavaScript代码。


例如

< script>

alert(z); //这将是一个错误,即一个例外

< / script>

< script>

alert( this.z);

alert(window.z); //两个等价,没有错误,但是window.z是

''undefined''

< / script>


为什么没有全局对象的''z'单独产生错误,

不应该是未定义的吗?例如,如果我做了


z = 0;


javascript解释器知道创建一个全局变量z

解决方案

yb写道:

例如
< script>


缺少必需的类型属性。

alert(z); //这将是一个错误,即一个例外
< / script>

< script>


见上文。

alert(this.z);
alert(window.z); //两个等价,没有错误,但是window.z是
''undefined''


这些陈述不等同。 `window''不需要引用全局对象的
。实际上,如果全局对象具有窗口属性,则

隐式地将第二个表达式等于this.window.z。

< / script>

为什么'z''没有全局对象就会产生错误,


因为它被指定了。参见ECMAScript 3 Final,8.7.1。

不应该是未定义的吗?


不,它不应该。见上文。

例如,如果我做了

z = 0;

javascript解释器知道创建一个全局变量z




这似乎只是如此。您可以通过

将范围链中的属性添加到全局对象;在全局范围内,结果将等于this.z = 0的
。但是,作用域链中的下一个对象

可能是一个宿主对象,而不允许扩充(或它的

属性被覆盖)。 IOW:赋予未声明的标识符
没有基础对象的
容易出错。始终使用`var''关键字声明诸如变量

(VariableStatement不使用范围链)

或使用基础对象引用。

PointedEars


谢谢,这澄清了我的几个问题。


我正在读大卫的JavaScript 4ed弗拉纳根。在第55页,它表示

范围链包含用''var''

关键字声明的变量。


抱歉,我正在引用一个不在线的文字,我打算对官方规格进行更深入的研究,但是有点困惑

Flanagan先生的文中的信息。


希望有人能澄清


yb写道:< blockquote class =post_quotes>寻找澄清未定义变量与JavaScript代码中的错误。


鉴于javascript有一个名为 - undefined的原始数据类型 -

会更好地谈论未声明的变量而不是未定义的

变量,因为声明的变量可能具有未定义的值。

例如
< script>
alert(z); //这将是一个错误,即一个例外
< / script>

< script>
alert(this.z);
alert(窗口) .z); //两个等价,没有错误,但是window.z是
''undefined''
< / script>


通过将范围

链解析并生成内部参考类型的实例作为
$ b $来评估不合格的标识符结果。命名未声明变量的标识符将导致

引用类型,其中 - null - 作为其Base对象,标识符

作为属性名称。


构造 - this.z - 和 - window.z - 是(点符号)属性

访问者。在评估

时,属性访问器也会产生Reference类型,但是如果属性访问器的部分位于点之前(或者在括号表示法属性访问器中打开方括号的
)没有

解析为Object类型(并且不能被类型转换为Object

类型)抛出异常。作为评估值的对象

在点成为Base对象之前属性访问器的一部分

由于对属性的评估而产生的Reference类型

访问器。


这是两个

参考类型中Base对象的值的差异,它解释了结果。任何使用具有null Base对象的Reference类型恢复某个值的b

抛出异常。虽然任何尝试使用具有非null base

对象的Reference类型读取

不存在的属性的值会导致 - undefined - 值。

为什么没有全局对象的z单独生成错误,不应该是未定义的吗?例如,如果我做了

z = 0;

javascript解释器知道创建一个全局变量z




这里的不同之处在于,操作是分配值

而不是其检索。如果尝试使用具有null Base对象的

引用类型来分配值,则解释器将使用

全局对象来代替不存在的Base对象。因此,对未申报的,不合格的标识符分配

会导致创建全局对象的

属性,并为该值分配值

属性。


严格来说这不代表全局变量的创建,

但是因为声明的全局变量是作为
全局对象声明的

全局变量与运行时分配的
赋值的全局对象的属性之间几乎没有实际区别。声明的全局变量不能被删除和

动态创建的全局对象的属性可以,但那就是

通常不会有明显的区别。


但是,优良作法是永远不要使用引用全局对象的

属性的标识符,而不首先将标识符声明为

a全局变量。这种''最佳实践''有助于避免和识别

脚本错误,因为很明显哪些标识符是为了b
引用全局变量,哪些可能只是泄漏超出他们的预期范围。
预定范围。它还避免了使用IE创建的全局对象的属性来命名碰撞问题,以表示具有

ID属性的DOM元素,并且在自然状态下对赋值具有抵抗力。


Richard。


Hi,

Looking for clarification of undefined variables vs. error in
JavaScript code.

e.g.
<script>
alert( z ); // this will be an error, i.e. an exception
</script>

<script>
alert( this.z );
alert( window.z ); // both equivalent, no error, but window.z is
''undefined''
</script>

why is it that ''z'' alone without the global object generates an error,
shouldn''t it also be undefined? for example if i did

z = 0;

the javascript interpreter knows to create a global variable z

解决方案

yb wrote:

e.g.
<script>
The required `type'' attribute is missing.
alert( z ); // this will be an error, i.e. an exception
</script>

<script>
See above.
alert( this.z );
alert( window.z ); // both equivalent, no error, but window.z is
''undefined''
The statements are not equivalent. `window'' does not need to refer to
the Global Object. In fact, if the Global Object has a `window'' property,
the second expression equals `this.window.z'' implicitly.
</script>

why is it that ''z'' alone without the global object generates an error,
Because it is specified so. See ECMAScript 3 Final, 8.7.1.
shouldn''t it also be undefined?
No, it should not. See above.
for example if i did

z = 0;

the javascript interpreter knows to create a global variable z



That only seems so. You may add a property to the Global Object through
the scope chain with this; in global context the result would be equivalent
to that of `this.z = 0'' then. However, the next object in the scope chain
may be a host object instead that does not allow for augmentation (or its
properties to be overwritten). IOW: assignment to undeclared identifiers
without base object are error prone. Always declare such as a variable
with the `var'' keyword (a VariableStatement does not use the scope chain)
or use a base object reference.
PointedEars


Thanks, that clarifies several questions I had.

I''m reading JavaScript 4ed by David Flanagan. On p.55, it indicates
that the scope chain includes the variables declared with ''var''
keywords.

Sorry I''m referencing a text that isn''t online, I plan to do a more
thorough study of the official specs but am a bit confused by the
information in Mr. Flanagan''s text.

Hope someone can clarify


yb wrote:

Looking for clarification of undefined variables vs.
error in JavaScript code.
Given that javascript has a primitive data type called - undefined - it
would be better to talk of undeclared variables rather than undefined
variables, as declared variables may have the value undefined.
e.g.
<script>
alert( z ); // this will be an error, i.e. an exception
</script>

<script>
alert( this.z );
alert( window.z ); // both equivalent, no error, but window.z is
''undefined''
</script>
An unqualified Identifier is evaluated by resolving it against the scope
chain and producing an instance of the internal Reference type as the
result. An Identifier naming an undeclared variable will result in a
Reference type that had - null - as its Base object and the Identifier
as the property name.

The constructs - this.z - and - window.z - are (dot notation) property
accessors. Property accessors also result in a Reference type when
evaluated, but if the part of the property accessor before the dot (or
opening square bracket in a bracket notation property accessor) does not
resolve as an Object type (and cannot be type-converted into an Object
type) an exception is thrown. The object that is the evaluated value of
the part of the property accessor before the dot becomes the Base object
of the Reference type that results from the evaluation of the property
accessor.

It is the difference in the value of the Base object in the two
Reference types that explains the difference in outcome. Any attempt to
recover a value with a Reference type that has a null Base object will
throw an exception. While any attempt to read the value of a
non-existent property with a Reference type that has a non-null base
object results in the - undefined - value.
why is it that ''z'' alone without the global object generates
an error, shouldn''t it also be undefined? for example if i did

z = 0;

the javascript interpreter knows to create a global variable z



The difference here is that the action is the assignment of a value
rather than its retrieval. Given an attempt to assign a value using a
Reference type that has a null Base object the interpreter uses the
global object in place of the non-existent Base object. Thus assignment
to an undeclared, unqualified Identifier results in the creation of a
property of the global object, and the assignment of a value to that
property.

Strictly this is does not represent the creation of a global variable,
but since declared global variables are created as properties of the
global object there is little practical distinction between a declared
global variable and a property of the global object created by
assignment at runtime. Declared global variable cannot be deleted and
dynamically created properties of the global object can be, but that is
not often a significant distinction.

However, it is good practice to never use an Identifier that refers to a
property of the global object without first declaring that Identifier as
a global variable. This ''best practice'' helps to avoid and identify
scripting errors as it becomes obvious which Identifiers are intended to
refer to global variables, and which may just be leaking out of their
intended scope. It also avoids naming collision issues with properties
of the global object created by IE to represent DOM Elements that have
ID attributes, and are resistant to assignment in their natural state.

Richard.


这篇关于未定义的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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