JavaScript undefined vs. null (翻译)

 

无意中看到一篇老外写的讨论javascript中undefined和null的文章,俺也学人家翻译一回。发现这英文不用也是要生锈的。


英文原文:http://saladwithsteve.com/2008/02/javascript-undefined-vs-null.html

JavaScript undefined vs. null

 

I was reading a modern, popular book on JavaScript last night and was disappointed by the handling of null. The author started out doing a lot of checking like:

昨天晚上,我在阅读一本javascript的畅销书,书中对null的处理让我失望。作者起初做了很多这样的检查:

 

if (foo == null) {   alert('foo is not set.'); }
Then told the reader that they could just remove the == null because javascript knows you mean "== null"

然后告诉读者,可以移除“== null”,因为javascript知道你这样写表示"== null"。

 

What?! This isn't why you don't check for equality with null. It's because foo == null doesn't even remotely do what most people think it does in this context.

说啥呢?这并不是你不检查等于null的原因。因为foo == null 甚至没有间接的做,在这个上下文中很多人以为它会做的(检查null)。

 

It's a commonly held belief that uninitialized properties in JavaScript are set to null as default values. People believe this mostly for 2 reasons: 1) foo == null returns true if foo is undefined and 2) authors don't teach JavaScript properly.

通常认为,在javascript中,未初始化的属性,默认被设置成null。这基于两个原因:1)如果foo是undefined,foo == null 返回true。2)作者没有正确的讲授javascript。

 

A property, when it has no definition, is undefined. Put that way, it's pretty obvious.

一个属性在未定义时,是undefiend。这种情况是很显然的。

 

null is an object. It's type is null. undefined is not an object, it's type is undefined. That part is less obvious.

null是一个object,它的类型是null。(注:这里可能是笔误。null的类型是object。) undefined不是一个object,它的类型是undefined。这部分不太明显。

 

The real trouble is that == does type coercion. === checks for both type and value and is the most intuitive form of equality in JavaScript, in my opinion.

真正的问题在于==做了类型转换。=== 同时检查类型和值,我认为这是javascript中表示相等的最直观的形式。

 

I fired up a Jash console to hopefully clear things up for you.

我启动Jash控制台,希望能为你们整理一下。

 

>> window.hello
null
>> window.hello.something
window.hello has no properties
>> window.hello == null
true
>> window.hello === null
false
>> window.hello === undefined
true
>> if (window.hello) { alert('truthy'); } else { alert('falsy'); } // will print falsy.
null
>> window.hello == undefined
true
>> null == undefined
true // there's the rub, sir.
>> null
null
>> undefined
null
>> typeof null
object
>> typeof undefined
undefined

So people write

if (foo == null) {
   foo = "Joe";
}
When what they really mean is
if (!foo) {
   foo = "Joe";
}
If you find yourself with a lot of null checks in your JavaScript, set aside some time and watch Douglas Crockford's "The JavaScript Programming Language" talk on Yahoo Video. It's part 1 of a 3-part series of excellent and enlightening talks.

如果你发现在你的javascript代码中有很多的null检查,抽点时间看看Yahoo Video上Douglas Crockford的《JavaScript编程语言》的演讲。它是一系列颇具启发性的优秀演讲中的第一部分。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值