`
eric_hwp
  • 浏览: 119912 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

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]
所有类型都会得到不同的字符串,几乎完美。


文章源自:烈火网,原文:http://www.veryhuo.com/a/view/52778.html

分享到:
评论

相关推荐

    JavaScript中Object.prototype.toString方法的原理

    在JavaScript中,想要判断某个对象值属于哪种内置类型,最靠谱的做法就是通过Object.prototype.toString方法. var arr = []; console.log(Object.prototype.toString.call(arr)) //[object Array] 本文要讲的就是,...

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

    这是一个十分常见的问题,用 typeof 是否能准确判断一个对象变量,答案是否定的,null 的结果...(无法区分自定义对象类型,自定义类型可以采用instanceof区分) console.log(Object.prototype.toString.call("jerry

    JavaScript中使用Object.prototype.toString判断是否为数组

    为什么要用Object.prototype.toString而不是Function.prototype.toString或者其它?这是和他们的toString解释方式有关系的。下面是ECMA中对Object.prototype.toString的解释: 代码如下: Object.prototype.toString...

    javascript Array.prototype.slice的使用示例

    经常的,可以看到Array.prototype.slice...var toString = Object.prototype.toString;(function() { var args = arguments; console.log(args, toString.call(args)); // [1, 2, 3] “[object Arguments]” var ar

    详解js类型判断

    js类型转换中typeof会将null也识别为object, 而且返回的类型比少,我们用Object.prototype.toString来实现 第一版 function isArray(value){ return Object.prototype.toString.call(value) === "[object Array]"; ...

    详解javascript常用工具类的封装

    return Object.prototype.toString.call(o).slice(8, -1) === 'String' } isNumber (o) { //是否数字 return Object.prototype.toString.call(o).slice(8, -1) === 'Number' } isObj (o) { //是否对象 return ...

    关于toString()为什么不能通过Object.toString()调用

    记录一些学习中遇到的问题,如果理解的有什么错误希望大佬们能帮我指正。 一、为什么有这个问题? 在学习他人写的js教程时,了解到获取一个值的数据类型时,使用type ...console.log(Object.prototype.toString.call(te

    js检测对象

    javascript检测对象类型使用object.prototype.toString.call(obj)

    JavaScript常用工具方法封装

    return Object.prototype.toString.call(o).slice(8, -1) === 'String' } isNumber (o) { //是否数字 return Object.prototype.toString.call(o).slice(8, -1) === 'Number' } isBoolean (o) { //是否boolean ...

    通过JS深度判断两个对象字段相同

    return Object.prototype.toString.call(obj)==='[object Object]'; }; /** * 判断此类型是否是Array类型 * @param {Array} arr */ function isArray(arr){ return Object.prototype.toString.call(arr)==='[object ...

    dialogAnalysis:对话分析

    Object.prototype.toString.call(RegExp) 对象自身属性检测 {}.hasOwnProperty(prop) Object.prototype.hasOwnProperty.call(o, prop); // 安全的检测 7.1Function 7.1.1 apply和call 7.2RegExp 7

    判断js中各种数据的类型方法之typeof与0bject.prototype.toString讲解

    提醒大家,Object.prototype.toString().call(param)返回的[object class]中class首字母是大写,像JSON这种甚至都是大写,所以,大家判断的时候可以都转换成小写,以防出错

    判断js的Array和Object的实现方法

    console.log(Object.prototype.toString.call(a)); //[object Array] var b = {'hello':'world'}; console.log(typeof b); // object console.log(b.toString()); // [object Object] console.lo

    前端开源库-es-tostring

    前端开源库-es-tostringes-toString,object.prototype.toString.call(obj)的缩写

    JS数据类型检测

    Object.prototype.toString.call :获取当前实例的所属类信息(最常用,最好用也是最难理解的) 作者:Ziye Zhang 链接:https://juejin.im/post/5b42e753e51d45190905daa1 来源:掘金 著作权归作者所有。商业转载...

    JavaScript中判断变量是数组、函数或是对象类型的方法

    考虑到兼容性,在没有此方法的浏览器中,可以使用 Object.prototype.toString.call(obj) === ‘[object Array]’替代。 代码如下: var isArray = Array.isArray || function(obj) {  return Object.prototype....

    判断数据类型的方法.html

    JS中判断数据类型的4种方法及其实例:typeof、instanceof、constructor、Object.prototype.toString.call();还有判断数据类型的函数

    开发用到的js封装方法(20种)

    return Object.prototype.toString.call(arr) ==='[object Array]'; } isArray([1,2,3]) //true 2、判断是否是一个函数(三种) function isFunction(fn) { return Object.prototype.toString.call(fn) ==='[object ...

    前端开源库-safe-tostring-pmb

    前端开源库-safe-tostring-pmbsafe-toString pmb,字符串(x),具有回退到object.prototype.toString.call(x)

Global site tag (gtag.js) - Google Analytics