“未定义的”值 [英] The "undefined" value

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

问题描述




我最近很困惑(这就是我的生活)关于

" undefined"值。我查看了常见问题解答,并没有看到任何关于

的信息。在 http://www.webreference.com/programm ... pt / gr / column9 /

他们说:


< snip>

未定义的属性

JavaScript的一个相对较新的添加是未定义的属性。

当你想测试一个变量是否已经初始化或者是b $ b时它是有用的没有。


var a;


if(a == undefined)a =" some value" ;;

< / snip>


这真的有效吗?不应该是if(a ==''undefined''的类型)?


对未定义的解释是什么?是和怎么检查它是

a好常见问题解答?


谢谢,


马蒂。

Hi,

I recently got very confused (well that''s my life) about the
"undefined" value. I looked in the FAQ and didn''t see anything about
it. On http://www.webreference.com/programm...pt/gr/column9/
they say:

<snip>
The undefined property
A relatively recent addition to JavaScript is the undefined property.
It''s useful when you want to test whether a variable has been
initialized or not.

var a;

if ( a == undefined ) a = "some value";
</snip>

Is this really valid? Shouldn''t it be "if (typeof a == ''undefined'') ?

Would an explanation on what "undefined" is and how to check for it be
a good entry for the FAQ?

Thanks,

Matty.

推荐答案

在ECMAScript第3规范中,''undefinde''出现在

上4.2语言概述

< quote>

属性是容纳

其他对象,原始值或方法的容器。原始值是以下内置

类型之一的

成员:Undefined,Null,Boolean,Number和String;一个对象是剩下的内置

类型Object的

成员;并且方法是通过

属性与对象相关联的函数。

< / quote>


在4.3中。 9我们可以阅读未定义的值:

< quote>

未定义的值是当变量没有分配给
时使用的原始值一个值。

< / quote>

然后在4.3.10 Undefined Type中我们看到:

< quote>

类型Undefined只有一个值,称为undefined。

< / quote>


,其中未定义一词是加粗......因此它是规范的一部分...因此

是的:''未完成''是一个值

(我们可以例如。也可以在10.1中阅读.3变量实例化

< quote>

在输入执行上下文时,

属性绑定到以下变量对象

订单:(...)

如果

调用者提供的参数值少于正式的价值

paramete rs,额外的正式

参数值undefined。如果两个或多个正式参数共享

相同的名称,因此

相同的属性,相应的属性将被赋予

提供的值对于最后一个参数

这个名字。如果调用者没有提供最后一个参数的值,那么

对应属性的值是未定义的。

< /引用>


这个''undefined''是来自JavaScript 1.3的值(我们记得

JavaScript 1.3符合ECMAScript第1版)。

原始值可以直接使用,包括:

- 未定义

- null

- 任何字符串

- 任何数字(双精度64位格式IEEE 754);

- true

- false(布尔值);

(当然也有文字,文字我们有整数

文字等等。)


typeof can使用,但你必须记住它有''错误'':

typeof(null)==" object"

typeof(new Array)== 对象

typeof(undefinded)==" undefinded"

var myVar;

type of(myVar)==" undefined"

var myVar1 = 1;

typeof(myVar1)==" number"

typeof (" mystr")==" string"或者类型mystr ==" string"

typeof true =" true"

typeof false =" false"


等等on ...

祝你好运。

In ECMAScript 3rd specs the word ''undefinde'' occurs on
4.2 Language Overview
<quote>
Properties are containers that hold
other objects, primitive values, or methods. A primitive value is a
member of one of the following built-in
types: Undefined, Null, Boolean, Number, and String; an object is a
member of the remaining built-in
type Object; and a method is a function associated with an object via a
property.
</quote>

In 4.3.9 Undefined Value we can read:
<quote>
The undefined value is a primitive value used when a variable has not
been assigned a value.
</quote>
and then in 4.3.10 Undefined Type we see:
<quote>
The type Undefined has exactly one value, called undefined.
</quote>

, where word ''undefined'' is bolded... so it is a part of spec... thus
yes: ''undefinded'' is a value
(we can eg. read also in 10.1.3 Variable Instantiation that
<quote>
On entering an execution context, the
properties are bound to the variable object in the following
order:(...)
If
the caller supplies fewer parameter values than there are formal
parameters, the extra formal
parameters have value undefined. If two or more formal parameters share
the same name, hence the
same property, the corresponding property is given the value that was
supplied for the last parameter
with this name. If the value of this last parameter was not supplied by
the caller, the value of the
corresponding property is undefined.
</quote>

This ''undefined'' is a value from JavaScript 1.3 (and we remember that
JavaScript 1.3 was comformant to ECMAScript 1rd release).
So primitive values can be used directly, this includes:
- undefined
- null
- any string
- any number (which is double-precision 64-bit format IEEE 754);
- true
- false (which are booleans);
(of course there are also literals, and in literals we have Integer
Literals and so on).

typeof can be used, but you must remember the ''bugs'' that it has:
typeof(null) == "object"
typeof(new Array) == "object"
typeof(undefinded) == "undefinded"
var myVar;
typeof(myVar) == "undefined"
var myVar1 = 1;
typeof(myVar1) == "number"
typeof("mystr") == "string" or typeof "mystr" == "string"
typeof true = "true"
typeof false = "false"

and so on...
Best regards.


matty写道:
matty wrote:
< ; snip>
未定义的属性
JavaScript的一个相对较新的添加是未定义的属性。
当你想测试一个变量是否已经初始化或者它是有用的不是。

var a;

if(a == undefined)a =" some value" ;;


这个说法是假的,即使是时间过去的荒谬高价值

从此刻到现在为止仍然是最近的。最值得注意的是,一个

可以很容易地分配未定义值(内置的唯一值

未定义类型),或者其他一些假值。重要的是,变量

和属性以及条件表达式仍将评估为true。


var a;

//这里将适用

a = void 0; //适用,因为`void''运算符求值为

//'undefined''值'undefined''属性引用

a = this.bla; //提供this.bla未定义

a = null; //因隐式类型转换而适用


//这里不适用

a = false;

a = 0;

a = NaN;


if(a == undefined)//如果支持'undefined''属性

{

alert(" undefined?");

}

else

{

alert(" defined?");

}


if(b == undefined)// break,因为b未声明且未定义

{

// ...

}


var b; //适用于以下条件


if(typeof b ==" undefined")//如果b被宣布为

或定义,几乎不会中断见下面

{

alert(" undefined?")

}

< / snip>

这真的有效吗?


取决于你所说的有效。它在语法上是正确的ECMAScript

兼容代码。它不应该在JavaScript 1.3+(NN 4.06 + [1998])

和JScript 5.5+(IE / Win 5.5+ [?])中出错,前提是a被声明为

或之前定义的。之前句子中的条件清楚地表明

该方法不能作为对其解释的适当测试

以上。

不应该它是不是if(typeof a ==''undefined'')?


这是用于确保脚本在大多数

脚本引擎中不会中断的方法,与第一种方法相比,不会破坏

具有非实例化的本地属性(例如未声明且未定义

变量)。自ECMAScript 1,

JavaScript 1.1(NN3 + [1996年8月])和JScript 1.0(IE / Win 3.0+ [?])支持此(`typeof''运算符)。

但是,正如我之前所写,'undefined''是_not_"未定义"。

这是一个特殊值。


如果我应该直截了当地说:你应该删除你引用的那个

网站的所有引用。那里的信息不仅过时了,

这是完全错误的。

会解释什么是未定义是和怎么检查
这是一个很好的常见问题解答?
<snip>
The undefined property
A relatively recent addition to JavaScript is the undefined property.
It''s useful when you want to test whether a variable has been
initialized or not.

var a;

if ( a == undefined ) a = "some value";
That statement is false, even for ridiculous high values for time passed
from the moment till now for still being "recent". Most notably, one
can easily assign the `undefined'' value (the sole value of the built-in
Undefined type), or some other false-values for that matter, to a variable
and property and the conditional expression would still evaluate to `true''.

var a;

// either will apply here
a = void 0; // applies because the `void'' operator evaluates to the
// `undefined'' value the `undefined'' property refers to
a = this.bla; // provided this.bla was not defined
a = null; // applies due to implicit type conversion

// either will not apply here
a = false;
a = 0;
a = NaN;

if (a == undefined) // provided the `undefined'' property is supported
{
alert("undefined?");
}
else
{
alert("defined?");
}

if (b == undefined) // breaks, since b was not declared and not defined
{
// ...
}

var b; // applies to the below condition

if (typeof b == "undefined") // almost never breaks if b was declared
or defined, see below
{
alert("undefined?")
}
</snip>

Is this really valid?
Depends on what you call "valid". It is syntactically correct ECMAScript
compliant code. It should not error in JavaScript 1.3+ (NN 4.06+ [1998])
and JScript 5.5+ (IE/Win 5.5+ [?]), provided that `a'' was either declared
or defined before. The condition in the sentence before clearly indicates
that the approach cannot serve as suitable test of what it is explained
above to be.
Shouldn''t it be "if (typeof a == ''undefined'') ?
That is the approach used to ensure the script does not break in most
script engines and, in contrast to the first approach, does not break
with non-instantiated local properties (such as undeclared and not defined
variables). This (the `typeof'' operator) is supported since ECMAScript 1,
JavaScript 1.1 (NN3+ [Aug. 1996]) and JScript 1.0 (IE/Win 3.0+ [?]).
However, as I wrote before, `undefined'' is _not_ "not defined".
It is a special value.

If I should put it bluntly: you should delete all references to that
Web site you quoted from. The information there is not only outdated,
it is utterly wrong.
Would an explanation on what "undefined" is and how to check for
it be a good entry for the FAQ?




我不这么认为,因为我不认为这是一个经常被问到的

这里的问题以及任何能够阅读和理解参考资料的人

和FAQ中指出的规格知道

支持的(低)水平与`typeof ... =='undefined"''相比。

PointedEars



I do not think so as I do not perceive it as a frequently asked
question here and anyone who can read and understand the references
and specifications pointed to in the FAQ knows the (low) level of
support for it compared to `typeof ... == "undefined"''.
PointedEars




Luke Matuszewski写道:

Luke Matuszewski wrote:
typeof可以使用,但你必须记住它有的''bug'':
typeof(null)==" object"
typeof (新数组)==" object"
typeof(undefinded)==" undefinded"
var myVar;
typeof(myVar)==" undefined"
var myVar1 = 1;
typeof(myVar1)==" number"
typeof(" mystr")==" string"或者类型mystr ==" string"
typeof true =" true"
typeof false =" false"
typeof can be used, but you must remember the ''bugs'' that it has:
typeof(null) == "object"
typeof(new Array) == "object"
typeof(undefinded) == "undefinded"
var myVar;
typeof(myVar) == "undefined"
var myVar1 = 1;
typeof(myVar1) == "number"
typeof("mystr") == "string" or typeof "mystr" == "string"
typeof true = "true"
typeof false = "false"




谢谢。但是有什么错误?

Matty。



Thank you. But what bugs?
Matty.


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

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