您的位置:首页 > 其它

ActionScript中null和undefined的区别 分享

2013-03-21 15:25 260 查看
http://forestinthesea.blog.sohu.com/33648909.html

The difference between null and undefined is that undefined means the variable was never initialized or has been deleted. You should never *set* a variable to undefined, you should always use null instead, otherwise you ruin the whole distinction between
an uninitialized variable and one that you have set to null.

null和undefined之间的区别在于,undefined意味着变量没有被初始化或者被删除。你永远都不能设置一个undefined的变量,应该使用null来代替,否则你将搞混淆一个未初始化的变量和一个你设置为null的变量之间的区别。

It is useful to have two different types of "no value assigned", because

you can tell the difference between a variable that is just currently

without a value, and one that has either never been assigned a value or has

been deleted. It is even more useful in complex datatypes.

尽管从字面上来看都是“尚未指定值”,但是null和undefined是有区别的。因为设置为null的变量只是当前没有值,这表示你可以以后赋予这个变量任何值,但是undefined的变量表示你永远不可能赋予这个变量任何值。这在复杂的数据类型中非常有用。

var o:Object;

trace(o); // undefined

o = new Object(); // initialize

trace(o); // [Object object]

o = String("blah"); // assign a primitive as an object

trace(o); // blah

o = String(""); // set to empty string

trace(o); // (empty string, *not* undefined)

o = null; // empty it

trace(o); // null

delete o; // destroy it

trace(o); // undefined

So you can see, it can be useful to be able to make the distinction between a variable that has been initialized and then emptied, and a variable that has never been initialized. The most common place to do this is in something like this:

所以可以看到,能够区别一个已经初始化但是置空的变量和一个尚未初始化的变量之间的区别是非常有用的。

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: