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]
所有类型都会得到不同的字符串,几乎完美。
思考:使用return obj == null ? String(obj) : class2type[obj.toString()] || "object" 也是可以的,作者是先将Object的tostring赋值给toString,
Object.prototype.toString.call()的更多相关文章
- 利用Object.prototype.toString方法,实现比typeof更准确的type校验
Object.prototype.toString方法返回对象的类型字符串,因此可以用来判断一个值的类型. 调用方法: Object.prototype.toString.call(value) 不同 ...
- instanceof, typeof, & Object.prototype.toString
/** * * @authors Your Name (you@example.org) * @date 2016-11-18 09:31:23 * @version $Id$ */instanceo ...
- 判断一个变量的类型Object.prototype.toString.call
var num = 1;alert(Object.prototype.toString.call(num)); // [object Number]var str = 'hudidit.com';al ...
- Object.prototype.toString.call() 区分对象类型
判断一个对象的类型: /** * 判断对象是否为数组 * @param {Object} source 待判断的对象 * @return {Boolean} true|false */ Object. ...
- Object.prototype.toString.call()进行类型判断
为什么类型判断用到Object.prototype.toString.call()进行类型判断,而不用typeof()呢? 然后翻了一下资料: Typeof 在使用 ]));/));));//[obj ...
- toStirng()与Object.prototype.toString.call()方法浅谈
一.toString()是一个怎样的方法?它是能将某一个值转化为字符串的方法.然而它是如何将一个值从一种类型转化为字符串类型的呢? 通过下面几个例子,我们便能获得答案: 1.将boolean类型的值转 ...
- JavaScript中toStirng()与Object.prototype.toString.call()方法浅谈
toStirng()与Object.prototype.toString.call()方法浅谈 一.toString()是一个怎样的方法?它是能将某一个值转化为字符串的方法.然而它是如何将一个值从一种 ...
- JavaScript:Object.prototype.toString方法的原理
在JavaScript中,想要判断某个对象值属于哪种内置类型,最靠谱的做法就是通过Object.prototype.toString方法. var arr = []; console.log(Obje ...
- 【JavaScript】Object.prototype.toString.call()进行类型判断
权声明:本文为博主原创文章,未经博主允许不得转载. op = Object.prototype, ostring = op.toString, ... function isFunction(it) ...
- Object.prototype.toString.call() 区分对象类型(判断对象类型)
在 JavaScript 里使用 typeof 来判断数据类型,只能区分基本类型,即 “number”,”string”,”undefined”,”boolean”,”object” 五种.对于数组. ...
随机推荐
- Pandas 处理丢失数据
处理丢失数据 import pandas as pd from pandas import Series, DataFrame import numpy as np 有两种丢失数据: 1. None ...
- 使用Eclipse打jar包 包含依赖jar包
1.在项目根目录新建MANIFEST.MF文件 //版本号 Manifest-Version: 1.0 //依赖jar包路径 多个用空格隔开 Class-Path: lib/commons-loggi ...
- Ubuntu16.04 下 hadoop的安装与配置(伪分布式环境)
一.准备 1.1创建hadoop用户 $ sudo useradd -m hadoop -s /bin/bash #创建hadoop用户,并使用/bin/bash作为shell $ sudo pass ...
- 数组长度为len,数组元素的范围是0到len-1,找出数组的重复元素
public static int findDuplicate(int[] nums) { int len = nums.length; //注意这里的for循环写法,在交换元素后,i没有增加,还要继 ...
- 【css3】nth-child
nth-child含义 :nth-child(an+b) 这个 CSS 伪类匹配文档树中在其之前具有 an+b-1 个兄弟节点的元素,其中 n 为正值或零值. 简单点说就是,这个选择器匹配那些在同系列 ...
- 010_动态语言与鸭子类型及python2和3的区别
一. 动态语言中经常提到鸭子类型,所谓鸭子类型就是:如果走起路来像鸭子,叫起来也像鸭子,那么它就是鸭子(If it walks like a duck and quacks like a duck, ...
- 1-tomcat简介
一.tomcate的目录结构说明: 1.bin:存放服务器启动和关闭的命令文件.2.conf:存放服务器的配置信息文件3.lib:存放服务器自身需要的所有jar文件,也称为全局jar文件(只要部署在当 ...
- github上传超过100mb文件怎么办
使用Git LFS 上传.Git lFS(Git Large File Storage) 可以上传超过100MB的文件,使用方式为: 下载安装Git LFS 打开git cmd 中间输入 账号和密码 ...
- Apollo 3.0 硬件与系统安装指南
https://github.com/ApolloAuto/apollo/blob/master/docs/quickstart/apollo_3_0_hardware_system_installa ...
- Linux中断管理 (2)软中断和tasklet
目录: <Linux中断管理> <Linux中断管理 (1)Linux中断管理机制> <Linux中断管理 (2)软中断和tasklet> <Linux中断管 ...