日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

null === undefined_【英】两个“非值”:undefined 和 null

發布時間:2025/3/12 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 null === undefined_【英】两个“非值”:undefined 和 null 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

本期英文由@Dr. Axel Rauschmayer分享。

英文從這開始~~

Most programming languages have only one value for “no value” or “empty reference”. For example, that value is null in Java. JavaScript has two of those special values: undefined and null. They are basically the same (something that will change with ECMAScript 6, as will be explained in the last post of this series), but they are used slightly differently.

undefined is assigned via the language itself. Variables that have not been initialized yet have this value:

> var foo;

> foo

undefined

Similarly, JavaScript assigns undefined to missing parameters:

> function id(x) { return x }

> id()

undefined

null is used by programmers to explicitly indicate that a value is missing. E.g. for JSON.stringify():

> console.log(JSON.stringify({ first: 'Jane' }, null, 4))

{

"first": "Jane"

}

Check: does a variable have a value?

If you want to know whether a variable v has a value, you normally have to check for both undefined and null. Fortunately, both values are truthy. Thus, checking for truthiness via if performs both checks at the same time:

if (v) {

// v has a value

} else {

// v does not have a value

}

You’ll see more examples of the above check in the post for quirk 5 about parameter handling. There is one caveat: this check also interprets false, -0, +0, NaN and '' as “no value”. If that isn’t what you want then you can’t use it. You have two choices.

Some people advocate lenient non-equality (!=) to check that v is neither undefined nor null:

if (v != null) {

// v has a value

} else {

// v does not have a value

}

However, that requires you to know that != considers null to be only equal to itself and to undefined. I prefer the more descriptive use of !==:

if (v !== undefined && v !== null) {

// v has a value

} else {

// v does not have a value

}

Performance-wise, all three checks shown in this section are more or less the same. Hence, which one you will end up using depends on your needs and your taste. Some minification tools even rewrite the last check to a check via !=.?

關于本文 作者:@Dr. Axel Rauschmayer 原文:https://2ality.com/2013/04/quirk-undefined.html

為你推薦

【第1244期】詳解Object.create(null)

【英】在JavaScript中使用查詢參數

總結

以上是生活随笔為你收集整理的null === undefined_【英】两个“非值”:undefined 和 null的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。