JavaScript/typeof:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
无编辑摘要
 
(未显示同一用户的1个中间版本)
第24行: 第24行:


== 实例 ==
== 实例 ==
{{Leftrightflex|<syntaxhighlight lang="javascript">
boolean
typeof Symbol()
{{Double-columns|<syntaxhighlight lang="javascript">
typeof function(){}
typeof true;
typeof class C{}
typeof false;
typeof []
typeof (2 > 1);
typeof {}
</syntaxhighlight>
typeof true
|{{Consoleout|<pre>'boolean'
typeof "str"
</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 null;
</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 new RegExp();
typeof Number;
typeof new Date();
typeof Array;
typeof new Error();
</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>"symbol"
</syntaxhighlight>
"function"
|{{Consoleout|<pre>'bigint'</pre>}}
"function"
"object"
"object"
"boolean"
"string"
"number"
"undefined"
"object"
"object"
"object"
"object"
"object"
"function"
"object"
"object"
"object"
"bigint"
"bigint"
</pre>}}
}}
}}
== null ==
== null ==
类型检测
类型检测
{{leftrightflex|<syntaxhighlight lang="javascript">!null&&typeof null === "object"</syntaxhighlight>|{{consoleout|<pre>true
{{Double-columns|<syntaxhighlight lang="javascript">!null&&typeof null === "object"</syntaxhighlight>|{{consoleout|<pre>true
</pre>}}}}
</pre>}}}}
== 原理 ==
== 原理 ==
第91行: 第130行:


== undefined 和 null ==
== undefined 和 null ==
{{Leftrightflex|<syntaxhighlight lang="javascript">
{{Double-columns|<syntaxhighlight lang="javascript">
undefined == null;
undefined == null;
undefined === null;
undefined === null;
第102行: 第141行:
}}
}}
== es6 ==
== es6 ==
{{leftrightflex|<syntaxhighlight lang="javascript">
{{Double-columns|<syntaxhighlight lang="javascript">
typeof new Set();
typeof new Set();
typeof new Map();
typeof new Map();