diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/null/index.html | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/null/index.html')
-rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/null/index.html | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/null/index.html b/files/zh-cn/web/javascript/reference/global_objects/null/index.html new file mode 100644 index 0000000000..b2c434ace0 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/null/index.html @@ -0,0 +1,93 @@ +--- +title: 'null' +slug: Web/JavaScript/Reference/Global_Objects/null +tags: + - JavaScript + - Literal + - Primitive +translation_of: Web/JavaScript/Reference/Global_Objects/null +--- +<div>{{jsSidebar("Objects")}}</div> + +<p>值 <code>null</code> 特指对象的值未设置。它是 JavaScript {{Glossary("Primitive", "基本类型")}} 之一,在布尔运算中被认为是<a href="https://developer.mozilla.org/en-US/docs/Glossary/Falsy">falsy</a>。</p> + +<div>{{EmbedInteractiveExample("pages/js/globalprops-null.html")}}</div> + + + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">null</pre> + +<h2 id="描述">描述</h2> + +<p>值 <code>null</code> 是一个字面量,不像 {{jsxref("Global_Objects/undefined","undefined")}},它不是全局对象的一个属性。<code>null</code> 是表示缺少的标识,指示变量未指向任何对象。把 <code>null</code> 作为尚未创建的对象,也许更好理解。在 API 中,<code>null</code> 常在返回类型应是一个对象,但没有关联的值的地方使用。</p> + +<pre class="brush: js">// foo 不存在,它从来没有被定义过或者是初始化过: +foo; +"ReferenceError: foo is not defined" + +// foo 现在已经是知存在的,但是它没有类型或者是值: +var foo = null; +foo; +null</pre> + +<h3 id="null_与_undefined_的不同点:"><code>null</code> 与 <code>undefined</code> 的不同点:</h3> + +<p>当检测 <code>null</code> 或 <code>undefined</code> 时,注意<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators">相等(==)与全等(===)两个操作符的区别</a> ,前者会执行类型转换:</p> + +<pre class="brush: js">typeof null // "object" (因为一些以前的原因而不是'null') +typeof undefined // "undefined" +null === undefined // false +null == undefined // true +null === null // true +null == null // true +!null //true +isNaN(1 + null) // false +isNaN(1 + undefined) // true +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">规范版本</th> + <th scope="col">规范状态</th> + <th scope="col">注解</th> + </tr> + <tr> + <td>{{SpecName('ES1')}}</td> + <td>{{Spec2('ES1')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-4.3.11', 'null value')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td></td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-null-value', 'null value')}}</td> + <td>{{Spec2('ES6')}}</td> + <td></td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-null-value', 'null value')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("javascript.builtins.null")}}</p> + +<h2 id="参见">参见</h2> + +<ul> + <li>{{jsxref("undefined")}}</li> + <li>{{jsxref("NaN")}}</li> +</ul> |