JavaScript undefined与null的区别

时间:2022-08-29 21:35:57
     作者:  阮一峰

日期: 2014年3月28日

大多数计算机语言,有且仅有一个表示"无"的值,比如,C语言的NULL,Java语言的null,Python语言的None,Ruby语言的nil。

有点奇怪的是,JavaScript语言居然有两个表示"无"的值:undefined和null。这是为什么?

JavaScript undefined与null的区别

一、相似性

在JavaScript中,将一个变量赋值为undefined或null,老实说,几乎没区别。

<code class=" language-javascript" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; font-family: Consolas,Monaco,'Andale Mono',monospace; font-size: 1.2em; word-spacing: normal; list-style-type: none; direction: ltr;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">
</span><span class="token keyword" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(0, 119, 170); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">var</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);"> a </span><span class="token operator" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(166, 127, 89); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">=</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);"> undefined</span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">;</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);">

</span><span class="token keyword" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(0, 119, 170); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">var</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);"> a </span><span class="token operator" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(166, 127, 89); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">=</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);"> </span><span class="token keyword" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(0, 119, 170); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">null</span></span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">;</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);">

</span></code>

上面代码中,a变量分别被赋值为undefined和null,这两种写法几乎等价。

undefined和null在if语句中,都会被自动转为false,相等运算符甚至直接报告两者相等。

<code class=" language-javascript" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; font-family: Consolas,Monaco,'Andale Mono',monospace; font-size: 1.2em; word-spacing: normal; list-style-type: none; direction: ltr;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">
</span><span class="token keyword" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(0, 119, 170); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">if</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);"> </span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">(</span></span><span class="token operator" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(166, 127, 89); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">!</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);">undefined</span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">)</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);">
console</span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">.</span></span><span class="token function" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(17, 17, 17); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">log</span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">(</span></span></span><span class="token string" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(102, 153, 0); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">'undefined is false'</span></span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">)</span></span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">;</span></span><span class="token comment" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(112, 128, 144); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">
// undefined is false
</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);">
</span><span class="token keyword" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(0, 119, 170); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">if</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);"> </span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">(</span></span><span class="token operator" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(166, 127, 89); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">!</span></span><span class="token keyword" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(0, 119, 170); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">null</span></span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">)</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);">
console</span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">.</span></span><span class="token function" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(17, 17, 17); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">log</span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">(</span></span></span><span class="token string" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(102, 153, 0); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">'null is false'</span></span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">)</span></span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">;</span></span><span class="token comment" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(112, 128, 144); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">
// null is false
</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);">
undefined </span><span class="token operator" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(166, 127, 89); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">==</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);"> </span><span class="token keyword" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(0, 119, 170); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">null</span></span><span class="token comment" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(112, 128, 144); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">
// true
</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);">
</span></code>

上面代码说明,两者的行为是何等相似!

既然undefined和null的含义与用法都差不多,为什么要同时设置两个这样的值,这不是无端增加JavaScript的复杂度,令初学者困扰吗?Google公司开发的JavaScript语言的替代品Dart语言,就明确规定只有null,没有undefined!

二、历史原因

最近,我在读新书《Speaking JavaScript》时,意外发现了这个问题的答案!

原来,这与JavaScript的历史有关。1995年JavaScript诞生时,最初像Java一样,只设置了null作为表示"无"的值。

根据C语言的传统,null被设计成可以自动转为0。

<code class=" language-javascript" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; font-family: Consolas,Monaco,'Andale Mono',monospace; font-size: 1.2em; word-spacing: normal; list-style-type: none; direction: ltr;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">
</span><span class="token function" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(17, 17, 17); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">Number</span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">(</span></span></span><span class="token keyword" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(0, 119, 170); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">null</span></span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">)</span></span><span class="token comment" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(112, 128, 144); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">
// 0
</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);">
</span><span class="token number" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 0, 85); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">5</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);"> </span><span class="token operator" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(166, 127, 89); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">+</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);"> </span><span class="token keyword" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(0, 119, 170); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">null</span></span><span class="token comment" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(112, 128, 144); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">
// 5
</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);">
</span></code>

但是,JavaScript的设计者Brendan Eich,觉得这样做还不够,有两个原因。

首先,null像在Java里一样,被当成一个对象。但是,JavaScript的数据类型分成原始类型(primitive)和合成类型(complex)两大类,Brendan Eich觉得表示"无"的值最好不是对象。

其次,JavaScript的最初版本没有包括错误处理机制,发生数据类型不匹配时,往往是自动转换类型或者默默地失败。Brendan Eich觉得,如果null自动转为0,很不容易发现错误。

因此,Brendan Eich又设计了一个undefined。

三、最初设计

JavaScript的最初版本是这样区分的:null是一个表示"无"的对象,转为数值时为0;undefined是一个表示"无"的原始值,转为数值时为NaN。

<code class=" language-javascript" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; font-family: Consolas,Monaco,'Andale Mono',monospace; font-size: 1.2em; word-spacing: normal; list-style-type: none; direction: ltr;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">
</span><span class="token function" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(17, 17, 17); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">Number</span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">(</span></span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);">undefined</span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">)</span></span><span class="token comment" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(112, 128, 144); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">
// NaN
</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);">
</span><span class="token number" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 0, 85); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">5</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);"> </span><span class="token operator" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(166, 127, 89); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">+</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);"> undefined</span><span class="token comment" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(112, 128, 144); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">
// NaN
</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);">
</span></code>

四、目前的用法

但是,上面这样的区分,在实践中很快就被证明不可行。目前,null和undefined基本是同义的,只有一些细微的差别。

null表示"没有对象",即该处不应该有值。典型用法是:

(1) 作为函数的参数,表示该函数的参数不是对象。

(2) 作为对象原型链的终点。

<code class=" language-javascript" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; font-family: Consolas,Monaco,'Andale Mono',monospace; font-size: 1.2em; word-spacing: normal; list-style-type: none; direction: ltr;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">
Object</span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">.</span></span><span class="token function" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(17, 17, 17); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">getPrototypeOf</span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">(</span></span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);">Object</span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">.</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);">prototype</span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">)</span></span><span class="token comment" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(112, 128, 144); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">
// null
</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);">
</span></code>

undefined表示"缺少值",就是此处应该有一个值,但是还没有定义。典型用法是:

(1)变量被声明了,但没有赋值时,就等于undefined。

(2) 调用函数时,应该提供的参数没有提供,该参数等于undefined。

(3)对象没有赋值的属性,该属性的值为undefined。

(4)函数没有返回值时,默认返回undefined。

<code class=" language-javascript" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; font-family: Consolas,Monaco,'Andale Mono',monospace; font-size: 1.2em; word-spacing: normal; list-style-type: none; direction: ltr;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">
</span><span class="token keyword" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(0, 119, 170); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">var</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);"> i</span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">;</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);">
i</span><span class="token comment" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(112, 128, 144); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);"> // undefined
</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);">
</span><span class="token keyword" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(0, 119, 170); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">function</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);"> </span><span class="token function" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(17, 17, 17); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">f</span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">(</span></span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);">x</span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">)</span></span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">{</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);">console</span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">.</span></span><span class="token function" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(17, 17, 17); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">log</span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">(</span></span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);">x</span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">)</span></span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">}</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);">
</span><span class="token function" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(17, 17, 17); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">f</span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">(</span></span></span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">)</span></span><span class="token comment" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(112, 128, 144); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);"> // undefined
</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);">
</span><span class="token keyword" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(0, 119, 170); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">var</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);"> o </span><span class="token operator" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(166, 127, 89); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">=</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);"> </span><span class="token keyword" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(0, 119, 170); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">new</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);"> </span><span class="token class-name" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(17, 17, 17); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">Object</span></span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">(</span></span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">)</span></span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">;</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);">
o</span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">.</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);">p</span><span class="token comment" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(112, 128, 144); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);"> // undefined
</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);">
</span><span class="token keyword" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(0, 119, 170); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">var</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);"> x </span><span class="token operator" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(166, 127, 89); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">=</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);"> </span><span class="token function" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(17, 17, 17); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">f</span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">(</span></span></span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">)</span></span><span class="token punctuation" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(153, 153, 153); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);">;</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);">
x</span><span class="token comment" style="margin: 0px; padding: 0px; border: currentColor; border-image: none; color: rgb(112, 128, 144); list-style-type: none;"><span style="font-size:24px;background-color: rgb(245, 242, 240);"> // undefined
</span></span><span style="font-size:24px;background-color: rgb(245, 242, 240);">
</span></code>

(完)

文档信息