JavaScript/typeof:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
第24行: 第24行:


== 实例 ==
== 实例 ==
boolean
{{Leftrightflex|<syntaxhighlight lang="javascript">
typeof true;
typeof false;
typeof (2 > 1);
</syntaxhighlight>
|{{Consoleout|<pre>'boolean'
</pre>}}
}}
string
{{Leftrightflex|<syntaxhighlight lang="javascript">
typeof "str";
typeof "";
</syntaxhighlight>
|{{Consoleout|<pre>'string'
</pre>}}
}}
number
{{Leftrightflex|<syntaxhighlight lang="javascript">
{{Leftrightflex|<syntaxhighlight lang="javascript">
typeof Symbol()
typeof function(){}
typeof class C{}
typeof []
typeof {}
typeof true
typeof "str"
typeof 1;
typeof 1;
typeof -1;
typeof 0;
typeof Math.PI;
typeof Infinity;
typeof NaN;
</syntaxhighlight>
|{{Consoleout|<pre>'number'
</pre>}}
}}
undefined
{{Leftrightflex|<syntaxhighlight lang="javascript">
typeof undefined;
typeof undefined;
typeof null;
</syntaxhighlight>
|{{Consoleout|<pre>'undefined'
</pre>}}
}}
 
object
{{Leftrightflex|<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>}}
}}
{{Leftrightflex|<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
{{Leftrightflex|<syntaxhighlight lang="javascript">
typeof Symbol();
</syntaxhighlight>
|{{Consoleout|<pre>'symbol'</pre>}}
}}
 
{{Leftrightflex|<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 ==
类型检测
类型检测