JavaScript/typeof:修订间差异
来自康健生活
无编辑摘要 |
|||
| (未显示同一用户的6个中间版本) | |||
| 第1行: | 第1行: | ||
== 引言 == | == 引言 == | ||
* 内置类型的检测正确(null 为"object")。 | |||
* 方法进行检测,返回 function。 | |||
* 对象进行检测,返回 object。 | |||
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
| 第9行: | 第13行: | ||
|- | |- | ||
| string || String 类型 | | string || String 类型 | ||
|- | |||
| symbol || Symbol 类型 | |||
|- | |||
| bigint || Bigint 类型 | |||
|- | |||
| function || Function 类型 | |||
|- | |- | ||
| object || 引用类型或 Null 类型 | | object || 引用类型或 Null 类型 | ||
|} | |} | ||
== 实例 == | == 实例 == | ||
{{ | boolean | ||
typeof | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
typeof | typeof true; | ||
typeof false; | |||
typeof | typeof (2 > 1); | ||
typeof {} | </syntaxhighlight> | ||
|{{Consoleout|<pre>'boolean' | |||
</pre>}} | |||
}} | |||
string | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
typeof "str"; | |||
typeof ""; | |||
</syntaxhighlight> | |||
|{{Consoleout|<pre>'string' | |||
</pre>}} | |||
}} | |||
number | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
typeof 1; | typeof 1; | ||
typeof -1; | |||
typeof 0; | |||
typeof Math.PI; | |||
typeof Infinity; | |||
typeof NaN; | |||
</syntaxhighlight> | |||
|{{Consoleout|<pre>'number' | |||
</pre>}} | |||
}} | |||
undefined | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
typeof undefined; | typeof undefined; | ||
typeof | </syntaxhighlight> | ||
|{{Consoleout|<pre>'undefined' | |||
</pre>}} | |||
}} | |||
object | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
typeof []; | |||
typeof {}; | |||
typeof new RegExp(); | |||
typeof new Date(); | |||
typeof new Error(); | |||
typeof new String(); | typeof new String(); | ||
typeof new Number(); | typeof new Number(); | ||
typeof new Array(); | typeof new Array(); | ||
typeof new Object(); | typeof new Object(); | ||
typeof new Boolean(); | |||
typeof null; | |||
</syntaxhighlight> | |||
|{{Consoleout|<pre>'object' | |||
</pre>}} | |||
}} | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
typeof function(){}; | |||
typeof class C{}; | |||
typeof new Function(); | typeof new Function(); | ||
typeof | typeof Number; | ||
typeof | typeof Array; | ||
typeof | </syntaxhighlight> | ||
|{{Consoleout|<pre>'function'</pre>}} | |||
}} | |||
symbol | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
typeof Symbol(); | |||
</syntaxhighlight> | |||
|{{Consoleout|<pre>'symbol'</pre>}} | |||
}} | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
typeof 1n; | typeof 1n; | ||
typeof BigInt("1");</syntaxhighlight> | typeof BigInt("1"); | ||
|{{Consoleout|<pre> | </syntaxhighlight> | ||
|{{Consoleout|<pre>'bigint'</pre>}} | |||
</pre>}} | |||
}} | }} | ||
== null == | == null == | ||
类型检测 | 类型检测 | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript">!null&&typeof null === "object"</syntaxhighlight>|{{consoleout|<pre>true | ||
</pre>}}}} | </pre>}}}} | ||
== 原理 == | == 原理 == | ||
| 第73行: | 第123行: | ||
|- | |- | ||
| 1 || 整数 || 示例 | | 1 || 整数 || 示例 | ||
|- | |||
| 000 || null || | |||
|- | |||
| || undefined || -2^30 整数 | |||
|} | |} | ||
== undefined 和 null == | == undefined 和 null == | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
undefined == null; | undefined == null; | ||
undefined === null; | undefined === null; | ||
| 第86行: | 第140行: | ||
0</pre>}} | 0</pre>}} | ||
}} | }} | ||
== es6 == | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
typeof new Set(); | |||
typeof new Map(); | |||
var foo = new Promise((resolve,reject)=>{}); | |||
typeof foo; | |||
var bar = new Proxy({},{}); | |||
typeof bar; | |||
</syntaxhighlight>|{{consoleout|<pre> | |||
"object" | |||
"object" | |||
"object" | |||
"object" | |||
</pre>}}}} | |||