Object.prototype.toString.call()和typeof()区别
js中判断数据类型都知道用tyoeof(),但是他只能判断object,boolean,function,string,number,undefined还有symbol(es6新增)这几种初步类型,比如new Date和null,它就只能是object。
console.log(typeof(new Date())); // object
console.log(typeof(null));// object
console.log(typeof([1,2,3]));// object
那么,我们使用Object.prototype.toString.call(),就可以做到
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(undefined));//[object Undefined]
console.log(Object.prototype.toString.call(null));//[object Null]
console.log(Object.prototype.toString.call({name: "jerry"}));//[object Object]
console.log(Object.prototype.toString.call(function(){}));//[object Function]
console.log(Object.prototype.toString.call([]));//[object Array]
console.log(Object.prototype.toString.call(new Date));//[object Date]
console.log(Object.prototype.toString.call(/\d/));//[object RegExp]
function Person(){}; console.log(Object.prototype.toString.call(new Person));//[object Object]
判断console.log(Object.prototype.toString.call(new Person) === '[object Object]'); //true
Object.prototype.toString.call()和typeof()区别的更多相关文章
- typeof 和 Object.prototype.toString.call 数据类型判断的区别
使用 typeof 来判断数据类型,只能区分基本类型,即 “number”,”string”,”undefined”,”boolean”,”object” 五种. 但 Object.prototype ...
- instanceof, typeof, & Object.prototype.toString
/** * * @authors Your Name (you@example.org) * @date 2016-11-18 09:31:23 * @version $Id$ */instanceo ...
- 利用Object.prototype.toString方法,实现比typeof更准确的type校验
Object.prototype.toString方法返回对象的类型字符串,因此可以用来判断一个值的类型. 调用方法: Object.prototype.toString.call(value) 不同 ...
- Object.prototype.toString & typeof
Object.prototype.toString & typeof Object.prototype.toString 获取某个对象属于哪种内置类型 typeof 得到某个对象的类型 差别 ...
- instance of type of object.prototype.tostring 区别
typeof typeof 是一个操作符,其右侧跟一个一元表达式,并返回这个表达式的数据类型. 返回的结果用该类型的字符串(全小写字母)形式表示,包括以下 6 种: number.boolea ...
- 类型判断----小白讲解typeof,instanceof,Object.prototype.toString.call()
1.typeof只能判断基本类型数据, 例子: typeof 1 // "number" typeof '1' // "string" typeof true ...
- JS四种判断数据类型的方法:typeof、instanceof、constructor、Object.prototype.toString.call()
1.typeof 1 console.log(typeof ""); //string 2 console.log(typeof 1); //number 3 console.lo ...
- typeof 、Object.prototype.toString和 instanceof
数据类型 js 基本类型包括:Undefined symbol null string boolean number js 引用类型包括:object array Date RegExp typeo ...
- typeof操作符,返回数据类型Array.isArray()、Object.prototype.toString.call()
源地址https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/typeof typeof操作符 // N ...
随机推荐
- martini-能量最小化参数(mdp文件)
1 ; 2 ; STANDARD MD INPUT OPTIONS FOR MARTINI 2.x 3 ; Updated 02 feb 2013 by DdJ 4 ; 5 ; for use wit ...
- tcpack---1简述
TCP重传机制 TCP要保证所有的数据包都可以到达,所以,必需要有重传机制. 超时重传机制 一种是不回ack,死等3,当发送方发现收不到3的ack超时后,会重传3.一旦接收方收到3后,会ack 回 4 ...
- dp背包 面试题 08.11. 硬币
https://leetcode-cn.com/problems/coin-lcci/ 硬币.给定数量不限的硬币,币值为25分.10分.5分和1分,编写代码计算n分有几种表示法.(结果可能会很大,你需 ...
- psycopg2模块安装问题
我的平台是win10(x64).python3.7,打算通过psycopg2模块来操作Greenplum数据库,我通过pip install psycopg2 安装了psycopg2模块,也提示安装成 ...
- t分布与t检验的一点理解
最近又遇到了t分布及t检验方面的内容,发现有些地方自己当初没有很明白,就又查了些资料,加深了一下自己的理解,这里也将自己的一些理解记录下来. 1. 理论基础--大数定理与中心极限定理 在 ...
- 处理ceph incompelete的经验
前言 最近已经见到几个环境出现过incompelete了,这个在很久以前Jewel正在合入mark-complete工具的时候就有做过类似的处理,但是随着处理的环境越来越多,这个地方还是有些需要注意的 ...
- Linxu (centos6.8)常见目录及文件解析
/etc/sysconfig/networ-scripts/ifcfg-eth0 第一块网卡配置 BOOTPROTO="dhcp" #启用地址协议,static静态协议,bo ...
- 2020-11-02(三年之约D92)-优秀不是一种行为,而是一种习惯
1.阅读:<软技能-代码之外的生存指南>- 第45章 培养习惯:刷新你的代码 成就我们的恰恰就是那些不断重复做的事情.因此,优秀不是一种行为,而是一种习惯--亚里士多德 习惯主要由三个要素 ...
- webug第十三关:XSS
第十三关:XSS 点击链接 xss,弹框框
- freshclam
1 3 * ** /usr/local/clamav/bin/freshclam 定时更新.定时杀毒