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:
所以可以看到,能够区别一个已经初始化但是置空的变量和一个尚未初始化的变量之间的区别是非常有用的。
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:
所以可以看到,能够区别一个已经初始化但是置空的变量和一个尚未初始化的变量之间的区别是非常有用的。
相关文章推荐
- ActionScript中null和undefined的区别 分享
- Javascript 与 ActionScript 中 null、NaN和undefined的区别
- JavaScript中Null与Undefined的区别解析
- Javascript 中 null、NaN和undefined的区别总结
- js判断undefined类型,undefined,null,NaN的区别
- undefined与null的区别
- Javascript 中 null、NaN和undefined的区别
- JavaScript中undefined与null的区别
- 描述 null 和 undefined 的区别
- js判断undefined类型,undefined,null,NaN的区别
- undefined与null的区别
- JavaScript中的null和undefined区别介绍
- 区别Javascript中的Null与Undefined
- 前端进阶(随手记)------undefined和null的区别
- js中undefined,null,NaN的区别
- 编程的细节之美——undefined与null的区别
- null和undefined的区别
- undefined类型 null类型 区别
- Javascript 中 null、NaN和undefined的区别
- undefined与null的区别分析