ActionScript中null和undefined的区别 分享

本文解释了JavaScript中null和undefined的区别,强调了在不同场景下使用null的重要性,以避免混淆变量状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值