用Object.prototype.toString.call() 区分对象类型

在 JavaScript 里使用 typeof 来判断数据类型,只能区分基本类型,即 “number”,”string”,”undefined”,”boolean”,”object” 五种。对于数组、函数、对象来说,其关系错综复杂,使用 typeof 都会统一返回 “object” 字符串。

要想区别对象、数组、函数单纯使用 typeof 是不行的。或者你会想到 instanceof 方法,例如下面这样:

var a = {};
var b = [];
var c = function () {};

//a b c 都是 Object 的实例
console.log(a instanceof Object) //true
console.log(b instanceof Object) //true
console.log(c instanceof Object) //true

//只有 Array 类型的 b 才是 Array 的实例
console.log(a instanceof Array) //false
console.log(b instanceof Array) //true
console.log(c instanceof Array) //false

//只有 Function 类型的 c 才是 Function 的实例
console.log(a instanceof Function) //false
console.log(b instanceof Function) //false
console.log(c instanceof Function) //true

从以上代码来看,要判断复合数据类型,可以如下判断:

//对象
(a instanceof Object) && !(a instanceof Function) && !(a instanceof Function)
//数组
(a instanceof Object) && (a instanceof Array)
//函数
(a instanceof Object) && (a instanceof Function)

更简便的方式,即是使用 Object.prototype.toString.call() 来确定类型,ECMA 5.1 中关于该方法的描述[1]是这样的:

When the toString method is called, the following steps are taken:
If the this value is undefined, return “[object Undefined]“.
If the this value is null, return “[object Null]“.
Let O be the result of calling ToObject passing the this value as the argument.
Let class be the value of the [[Class]] internal property of O.
Return the String value that is the result of concatenating the three Strings “[object ", class, and "]“.

由于 JavaScript 中一切都是对象,任何都不例外,对所有值类型应用 Object.prototype.toString.call() 方法结果如下:

console.log(Object.prototype.toString.call(123)) //[object Number]
console.log(Object.prototype.toString.call(‘123‘)) //[object String]
console.log(Object.prototype.toString.call(undefined)) //[object Undefined]
console.log(Object.prototype.toString.call(true)) //[object Boolean]
console.log(Object.prototype.toString.call({})) //[object Object]
console.log(Object.prototype.toString.call([])) //[object Array]
console.log(Object.prototype.toString.call(function(){})) //[object Function]

所有类型都会得到不同的字符串,几乎完美。

时间: 2024-08-23 07:10:14

用Object.prototype.toString.call() 区分对象类型的相关文章

Object.prototype.toString.call() 区分对象类型

在 JavaScript 里使用 typeof 来判断数据类型,只能区分基本类型,即 “number”,”string”,”undefined”,”boolean”,”object” 五种.对于数组.函数.对象,使用 typeof 都会统一返回 “object” 字符串. 有这样一种方法,即使用 Object.prototype.toString.call() 来确定类型. 由于 JavaScript 中一切都是对象,任何都不例外,对所有值类型应用 Object.prototype.toStri

用Object.prototype.toString()来检测对象的类型

昨天遇到要检测数据类型的情况,一般可以用typeof,但只能区分基本类型,即 "number","string","undefined","boolean","object" 五种. 对于数组.函数.对象来说,其关系错综复杂,使用 typeof 都会统一返回 "object" 字符串. 不过还有种方法貌似逼格有点高,那就是Object.prototype.toString(). 具体如下

JS区分对象类型

Object.prototype.toString.call() 区分对象类型 在JavaScript中数据类型分为:1.基本类型,2.引用类型 基本类型:Undefined,Boolean,String,Number,Null 引用类型:Object (Array,Date,RegExp,Function) var a = 'hello world'; var b = []; var c = function(){}; 1 2 3 我们用不同的判断类型的方法来判断上面三个变量的类型:(编译工具

从toString()方法到Object.prototype.toString.call()方法

一.toString方法和Object.prototype.toSting.call()的区别 var arr=[1,2]; 直接对一个数组调用toString()方法, console.log(arr.toString());    //输出1,2 现在通过call方法指定arr数组为Object.prototype对象中的toString方法的上下文. console.log(Object.prototype.toString.call(arr));    //输出[Object Array

用Object.prototype.toString.call(obj)检测对象类型原因分析

用Object.prototype.toString.call(obj)检测对象类型原因分析 更新时间:2018年10月11日 08:46:33   投稿:laozhang    我要评论 在本篇文章里我们给大家剖析了用Object.prototype.toString.call(obj)检测对象类型的原因,需要的朋友们可以学习下. 这是一个十分常见的问题,用 typeof 是否能准确判断一个对象变量,答案是否定的,null 的结果也是 object,Array 的结果也是 object,有时候

js中通过Object.prototype.toString方法----精确判断对象的类型

判断是否为函数 function isFunction(it) {        return Object.prototype.toString.call(it) === '[object Function]';    } 判断是否为数组: function isArray(o) {   return Object.prototype.toString.call(o) === '[object Array]';  } 由于 JavaScript 中一切都是对象,任何都不例外,对所有值类型应用

判断对象类型 typeof instanceof Object.prototype.tostring()

常见的有三种方法  1, typeof  2, instance of   3, object.prototype.toString.apply(); 1,typeof  typeof 运算符 typeof 是一元运算符,返回结果是一个说明运算数类型的字符串.如:"number","string","boolean","object","function","undefined"(可用于

数据类型的判断 --Object.prototype.toString.call(obj)精准检测对象类型

数据类型的判断 typeof typeof返回一个表示数据类型的字符串,返回结果包括:number.boolean.string.symbol.object.undefined.function等7种数据类型,但不能判断null.array等 typeof Symbol(); // symbol 有效 typeof ''; // string 有效 typeof 1; // number 有效 typeof true; //boolean 有效 typeof undefined; //undef

为什么用Object.prototype.toString.call(obj)检测对象类型?

console.log(Object.prototype.toString.call("jerry"));//[object String]console.log(Object.prototype.toString.call(12));//[object Number]console.log(Object.prototype.toString.call(true));//[object Boolean]console.log(Object.prototype.toString.call