Revision 645145 of null

  • Revision slug: Web/JavaScript/Reference/Global_Objects/null
  • Revision title: null
  • Revision id: 645145
  • Created:
  • Creator: ziyunfei
  • Is current revision? No
  • Comment

Revision Content

{{JSObjectsQLAlpha()}}

概述

值 null 是一个 JavaScript 字面量,表示空值(null or an "empty" value),即没有对象被呈现(no object value is present)。它是 JavaScript {{Glossary("Primitive", "原始值")}} 之一。

语法

null

描述

null 是一个字面量(而不是全局对象的一个属性,undefined 是)。在 APIs 中,null 常被放在期望一个对象,但是不引用任何对象的参数位置。当检测 null 或 undefined 时,注意相等(==)与全等(===)两个操作符的区别 (前者会执行类型转换)。

// foo does not exist, it is not defined and has never been initialized:
> foo
"ReferenceError: foo is not defined"

// foo is known to exist now but it has no type or value:
> var foo = null; foo
"null"
 
 
 
 
 
 
 

null 与 undefined 的不同点:

typeof null        // object (bug in ECMAScript, should be null)
typeof undefined   // undefined
null === undefined // false
null  == undefined // true
 
 
 
 

规范

规范版本 规范状态 注解
ECMAScript 1st Edition. Standard Initial definition.
{{SpecName('ES5.1', '#sec-4.3.11', 'null value')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-null-value', 'null value')}} {{Spec2('ES6')}}  

浏览器兼容性

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}

相关链接

  • {{jsxref("Global_Objects/undefined", "undefined")}}
  • {{jsxref("Global_Objects/NaN", "NaN")}}

Revision Source

<div>
 {{JSObjectsQLAlpha()}}</div>
<h2 id=".E6.A6.82.E8.BF.B0" style="margin-bottom: 20px; line-height: 30px;">概述</h2>
<p>值&nbsp;<code>null</code>&nbsp;是一个 JavaScript 字面量,表示空值(null or an "empty" value),即没有对象被呈现(no object value is present)。它是 JavaScript {{Glossary("Primitive", "原始值")}} 之一。</p>
<h2 id="Syntax" name="Syntax" style="margin-bottom: 20px; line-height: 30px;">语法</h2>
<pre class="syntaxbox  language-html" data-prism-prevent-line-number="1" style="margin-bottom: 0px; padding: 1em; border-left-width: 6px; border-left-style: solid; border-left-color: rgba(0, 83, 159, 0.65098); font-family: Consolas, Monaco, 'Andale Mono', monospace; font-size: 14px; direction: ltr; white-space: normal; word-break: normal; tab-size: 4; text-shadow: none; background-color: rgba(212, 221, 228, 0.498039);">
<code class="language-html" style="font-family: Consolas, Monaco, 'Andale Mono', monospace; direction: ltr; word-spacing: normal; word-break: normal; tab-size: 4; color: inherit; text-shadow: none;">null</code></pre>
<h2 id="Description" name="Description" style="margin-bottom: 20px; line-height: 30px;">描述</h2>
<p><code>null</code>&nbsp;是一个字面量(而不是全局对象的一个属性,<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined" title="/en-US/docs/JavaScript/Reference/Global_Objects/undefined"><code>undefined</code></a>&nbsp;是)。在 APIs 中,<code>null</code>&nbsp;常被放在期望一个对象,但是不引用任何对象的参数位置。当检测 null 或 undefined 时,注意<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators">相等(==)与全等(===)两个操作符的区别</a>&nbsp;(前者会执行类型转换)。</p>
<pre class="brush: js">
// foo does not exist, it is not defined and has never been initialized:
&gt; foo
"ReferenceError: foo is not defined"

// foo is known to exist now but it has no type or value:
&gt; var foo = null; foo
"null"</pre>
<div class="line-number" data-start="1" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; pointer-events: none; line-height: inherit; top: 0px; background: 0px 0px;">
 &nbsp;</div>
<div class="line-number" data-start="2" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; pointer-events: none; line-height: inherit; top: 19px; background: 0px 0px;">
 &nbsp;</div>
<div class="line-number" data-start="3" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; pointer-events: none; line-height: inherit; top: 38px; background: 0px 0px;">
 &nbsp;</div>
<div class="line-number" data-start="4" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; pointer-events: none; line-height: inherit; top: 57px; background: 0px 0px;">
 &nbsp;</div>
<div class="line-number" data-start="5" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; pointer-events: none; line-height: inherit; top: 76px; background: 0px 0px;">
 &nbsp;</div>
<div class="line-number" data-start="6" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; pointer-events: none; line-height: inherit; top: 95px; background: 0px 0px;">
 &nbsp;</div>
<div class="line-number" data-start="7" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; pointer-events: none; line-height: inherit; top: 114px; background: 0px 0px;">
 &nbsp;</div>
<h3 id="null.C2.A0.E4.B8.8E.C2.A0undefined_.E7.9A.84.E4.B8.8D.E5.90.8C.E7.82.B9.EF.BC.9A" style="line-height: 24px;"><code>null</code>&nbsp;与&nbsp;<code>undefined</code> 的不同点:</h3>
<pre class="brush: js">
typeof null&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // object (bug in ECMAScript, should be null)
typeof undefined&nbsp;&nbsp; // undefined
null === undefined // false
null&nbsp; == undefined // true</pre>
<div class="line-number" data-start="1" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; pointer-events: none; line-height: inherit; top: 0px; background: 0px 0px;">
 &nbsp;</div>
<div class="line-number" data-start="2" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; pointer-events: none; line-height: inherit; top: 19px; background: 0px 0px;">
 &nbsp;</div>
<div class="line-number" data-start="3" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; pointer-events: none; line-height: inherit; top: 38px; background: 0px 0px;">
 &nbsp;</div>
<div class="line-number" data-start="4" style="margin-top: 1em; position: absolute; left: 0px; right: 0px; pointer-events: none; line-height: inherit; top: 57px; background: 0px 0px;">
 &nbsp;</div>
<h2 id=".E8.A7.84.E8.8C.83" style="margin-bottom: 20px; line-height: 30px;">规范</h2>
<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范版本</th>
   <th scope="col">规范状态</th>
   <th scope="col">注解</th>
  </tr>
  <tr>
   <td>ECMAScript 1st Edition.</td>
   <td>Standard</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-4.3.11', 'null value')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-null-value', 'null value')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>
<h2 id=".E6.B5.8F.E8.A7.88.E5.99.A8.E5.85.BC.E5.AE.B9.E6.80.A7" style="margin-bottom: 20px; line-height: 30px;">浏览器兼容性</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th style="line-height: 16px;">Feature</th>
    <th style="line-height: 16px;">Chrome</th>
    <th style="line-height: 16px;">Firefox (Gecko)</th>
    <th style="line-height: 16px;">Internet Explorer</th>
    <th style="line-height: 16px;">Opera</th>
    <th style="line-height: 16px;">Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th style="line-height: 16px;">Feature</th>
    <th style="line-height: 16px;">Android</th>
    <th style="line-height: 16px;">Chrome for Android</th>
    <th style="line-height: 16px;">Firefox Mobile (Gecko)</th>
    <th style="line-height: 16px;">IE Mobile</th>
    <th style="line-height: 16px;">Opera Mobile</th>
    <th style="line-height: 16px;">Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<h2 id="See_also" name="See_also" style="margin-bottom: 20px; line-height: 30px;">相关链接</h2>
<ul>
 <li>{{jsxref("Global_Objects/undefined", "undefined")}}</li>
 <li>{{jsxref("Global_Objects/NaN", "NaN")}}</li>
</ul>
Revert to this revision