JS基础-数据类型判断typeof、instanceof、Object.prototype.toString
typeof用在基本数据类型和函数时,返回其对应类型的描述,对于引用类型都返回为object.instanceof无法判断基本数据类型,对于引用类型数据,返回其其对应类型。Object.prototype.toString无论基本数据类型还是引用类型返回其对应类型。
对应测试结果如下:
typeof test |
instanceof |
Object.prototype.toString.call(test) |
|
| var test = 'xuriliang'; | string | test instanceof String //false |
[object String] |
| var test = 27; | number | test instanceof Number //false |
[object Number] |
| var test = true; | boolean | test instanceof Boolean //false |
[object Boolean] |
| var test = [1,2,3]; | object | test instanceof Array //true |
[object Array] |
test instanceof Object //true |
|||
| var test = null; | object | test instanceof Object //false |
[object Null] |
| var test = undefined; | undefined | test instanceof Object //false |
[object Undefined] |
| var test = new String('xuriliang') | object | test instanceof String //true |
[object String] |
test instanceof Object //true |
|||
| var test = new Number(27) | object | test instanceof Number //true |
[object Number] |
test instanceof Object //true |
|||
| var test = new Boolean(true) | object | test instanceof Boolean //true |
[object Boolean] |
test instanceof Object //true |
|||
| var test = new Array(1,2,3) | object | test instanceof Array //true |
[object Array] |
test instanceof Object //true |
|||
| var test = function(){} | function | test instanceof Function //true |
[object Function] |
test instanceof Object //true |
|||
| var test = /d/ | object | test instanceof RegExp //true |
[object RegExp] |
test instanceof Object //true |
JS基础-数据类型判断typeof、instanceof、Object.prototype.toString的更多相关文章
- 类型判断----小白讲解typeof,instanceof,Object.prototype.toString.call()
1.typeof只能判断基本类型数据, 例子: typeof 1 // "number" typeof '1' // "string" typeof true ...
- js变量类型判断 严格通用 Object.prototype.toString.call()
Object.prototype.toString.call()判断结果: Object.prototype.toString.call(true) "[object Boolean]&qu ...
- typeof()与Object.prototype.toString.call()
用typeof方法只能初步判断number string undefined boolean object function symbol这几种初步类型 使用Object.prototype.toSt ...
- typeof 和 Object.prototype.toString.call 数据类型判断的区别
使用 typeof 来判断数据类型,只能区分基本类型,即 “number”,”string”,”undefined”,”boolean”,”object” 五种. 但 Object.prototype ...
- 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 ...
- 深入剖析JavaScript中的数据类型判断(typeof instanceof prototype.constructor)
关于JavaScript中的类型判断,我想大部分JavaScripter 都很清楚 typeof 和 instanceof,却很少有人知道 constructor,以及constructor与前面二 ...
- 使用Object.prototype.toString.call()方法精确判断对象的类型
在JavaScript里使用typeof判断数据类型,只能区分基本类型,即:number.string.undefined.boolean.object. 对于null.array.function. ...
- JavaScript instanceof深度剖析以及Object.prototype.toString.call()使用
本文由segementfalt上的一道instanceof题引出: var str = new String("hello world"); console.log(str ins ...
随机推荐
- 关于开箱即用的文档静态网站生成器VuePress
关于VuePress 一个由Vue驱动的静态文档网站生成框架,具有开箱即用的优点. 给项目添加.gitignore .gitignore是git用来排除目录的清单,我们把以下目录加入其中,以便每次操作 ...
- 24、Keepalived高可用介绍
24.1.什么是keepalived: Keepalived 软件起初是专为 LVS 负载均衡软件设计的,用来管理并监控 LVS 集群系统中各个服务节点的状态,后来又加入了可以实现高可用的 VRRP ...
- 洞悉Redis技术内幕:缓存,数据结构,并发,集群与算法
"为什么这个功能用不了?" 程序员:"清一下缓存" 上篇洞悉系列文章给大家详细介绍了MySQL的存储内幕:洞悉MySQL底层架构:游走在缓冲与磁盘之间.既然聊过 ...
- Tars | Win10下Docker部署TarsJava(SpringBoot)全过程及踩坑记录
@ 目录 前言 1. 相关环境版本: 坑点一:VMware与Win10 Docker冲突 坑点二:20.版本TarsJava(SpringBoot)依赖文件缺失 2. Docker安装: 坑点三:Do ...
- SpringBoot | 1.3 约定编程Spring AOP
前言 前面聊过Spring的一个很重要的概念,IoC控制反转,接下来就是AOP了: 1. AOP切面编程 面向切面编程,是利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度 ...
- [心得体会]Spring注解基本使用方法
涨知识系列 Environment environment = context.getEnvironment(); 在Spring中所有被加载到spring中的配置文件都会出现在这个环境变量中, 其中 ...
- 使用Gradle构建多模块SpringBoot项目
使用Gradle构建多模块SpringBoot项目 本项目使用Gradle构建SpringBoot项目,将不同的业务进行不同的模块划分(不做微服务与分布式架构); - 编辑器:Intellij IDE ...
- 什么是BSE
BSE (bridge system engineer) 是外包开发人员和客户之前的桥梁. 主要是将客户的需求准确的理解并传达给外包的开发人员,一般情况下也兼开发的 leader 工作. 参考: ht ...
- 2012年第三届蓝桥杯C/C++程序设计本科B组省赛题目 海盗比酒量 结果填空
** 一.题目 ** 海盗比酒量 有一群海盗(不多于20人),在船上比拼酒量.过程如下:打开一瓶酒,所有在场的人平分喝下,有几个人倒下了.再打开一瓶酒平分,又有倒下的,再次重复- 直到开了第4瓶酒,坐 ...
- Python获取list中指定元素的索引
在平时开发过程中,经常遇到需要在数据中获取特定的元素的信息,如到达目的地最近的车站,橱窗里面最贵的物品等等.怎么办?看下面 方法一: 利用数组自身的特性 list.index(target), 其中a ...