JavaScript/Object.prototype.toString.call:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
小 文字替换 -“{{leftrightflex|”替换为“{{Double-columns|”
 
(未显示同一用户的9个中间版本)
第1行: 第1行:
== 引言 ==
==引言==
== 示例 ==
==示例==
{{leftrightflex|<syntaxhighlight lang="javascript">
{{Double-columns|<syntaxhighlight lang="javascript">
Object.prototype.toString.call([]);
Object.prototype.toString.call([]);
Object.prototype.toString.call({});
Object.prototype.toString.call({});
Object.prototype.toString.call('');
Object.prototype.toString.call('');
Object.prototype.toString.call(1);
Object.prototype.toString.call(1);
Object.prototype.toString.call(NaN);
Object.prototype.toString.call(null);
Object.prototype.toString.call(null);
Object.prototype.toString.call(undefined);
Object.prototype.toString.call(undefined);
Object.prototype.toString.call(true);</syntaxhighlight>|{{consoleout|<pre>
Object.prototype.toString.call(true);
Object.prototype.toString.call(function(){});
Object.prototype.toString.call(new Date());
Object.prototype.toString.call(/[1-9]at/gi);
Object.prototype.toString.call(JSON);
Object.prototype.toString.call(1n);
Object.prototype.toString.call(Symbol());
Object.prototype.toString.call(new Map());
Object.prototype.toString.call(new Set());
var puz = new Promise((resolve,reject)=>{});
Object.prototype.toString.call(puz);
var xyz = new Proxy({},{});
Object.prototype.toString.call(xyz);</syntaxhighlight>|{{consoleout|<pre>
"[object Array]"
"[object Array]"
"[object Object]"
"[object Object]"
"[object String]"
"[object String]"
"[object Number]"
"[object Number]"
"[object Number]"
"[object Null]"
"[object Null]"
"[object Undefined]"
"[object Undefined]"
"[object Boolean]"
"[object Boolean]"
"[object Function]"
"[object Date]"
"[object RegExp]"
"[object JSON]"
"[object BigInt]"
"[object Symbol]"
"[object Map]"
"[object Set]"
"[object Promise]"
"[object Object]"
</pre>}}}}
==原生函数==
{{Double-columns|<syntaxhighlight lang="javascript">
Object.prototype.toString.call(new String());
Object.prototype.toString.call(new Number());
Object.prototype.toString.call(new Boolean());
Object.prototype.toString.call(new Array());
Object.prototype.toString.call(new Object());
Object.prototype.toString.call(new Function());
Object.prototype.toString.call(new RegExp());
Object.prototype.toString.call(new Date());
Object.prototype.toString.call(new Error());
</syntaxhighlight>|{{consoleout|<pre>"[object String]"
"[object Number]"
"[object Boolean]"
"[object Array]"
"[object Object]"
"[object Function]"
"[object RegExp]"
"[object Date]"
"[object Error]"
</pre>}}}}
</pre>}}}}


== 判断 ==
==判断==
判断原生JSON对象
<syntaxhighlight lang="javascript">
var isNativeJSON = window.JSON && Object.prototype.toString.call(JSON);</syntaxhighlight>

2026年11月26日 (四) 05:20的最新版本

引言

示例

Object.prototype.toString.call([]);
Object.prototype.toString.call({});
Object.prototype.toString.call('');
Object.prototype.toString.call(1);
Object.prototype.toString.call(NaN);
Object.prototype.toString.call(null);
Object.prototype.toString.call(undefined);
Object.prototype.toString.call(true);
Object.prototype.toString.call(function(){});
Object.prototype.toString.call(new Date());
Object.prototype.toString.call(/[1-9]at/gi);
Object.prototype.toString.call(JSON);
Object.prototype.toString.call(1n);
Object.prototype.toString.call(Symbol());
Object.prototype.toString.call(new Map());
Object.prototype.toString.call(new Set());
var puz = new Promise((resolve,reject)=>{});
Object.prototype.toString.call(puz);
var xyz = new Proxy({},{});
Object.prototype.toString.call(xyz);
"[object Array]"
"[object Object]"
"[object String]"
"[object Number]"
"[object Number]"
"[object Null]"
"[object Undefined]"
"[object Boolean]"
"[object Function]"
"[object Date]"
"[object RegExp]"
"[object JSON]"
"[object BigInt]"
"[object Symbol]"
"[object Map]"
"[object Set]"

"[object Promise]"

"[object Object]"

原生函数

Object.prototype.toString.call(new String());
Object.prototype.toString.call(new Number());
Object.prototype.toString.call(new Boolean());
Object.prototype.toString.call(new Array());
Object.prototype.toString.call(new Object());
Object.prototype.toString.call(new Function());
Object.prototype.toString.call(new RegExp());
Object.prototype.toString.call(new Date());
Object.prototype.toString.call(new Error());
"[object String]"
"[object Number]"
"[object Boolean]"
"[object Array]"
"[object Object]"
"[object Function]"
"[object RegExp]"
"[object Date]"
"[object Error]"

判断

判断原生JSON对象

var isNativeJSON = window.JSON && Object.prototype.toString.call(JSON);