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 ...
随机推荐
- 图解 Redis | 差点崩溃了,还好有主从复制
大家好,我是小林哥,又来图解 Redis 啦. 我在前两篇已经给大家图解了 AOF 和 RDB,这两个持久化技术保证了即使在服务器重启的情况下也不会丢失数据(或少量损失). 不过,由于数据都是存储在一 ...
- JavaScript实现的7种排序算法
所谓排序算法,即通过特定的算法因式将一组或多组数据按照既定模式进行重新排序.这种新序列遵循着一定的规则,体现出一定的规律,因此,经处理后的数据便于筛选和计算,大大提高了计算效率.对于排序,我们首先要求 ...
- Redis配置统计字典
本章将对Redis的系统状态信息(info命令结果)和Redis的所有配置(包括Standalone.Sentinel.Cluster三种模式)做一个全面的梳理,希望本章能够成为Redis配置统计字典 ...
- 入门Kubernetes - 滚动升级/回滚
一.前言 上一篇文章中对yaml文件格式进行了解,并对k8s中各种主要资源通过yaml创建时的定义模板.接来下就进一步学习k8s的各种特点.并应用在示例中. 接下来先实现.Net Core Api程序 ...
- 24、Keepalived高可用介绍
24.1.什么是keepalived: Keepalived 软件起初是专为 LVS 负载均衡软件设计的,用来管理并监控 LVS 集群系统中各个服务节点的状态,后来又加入了可以实现高可用的 VRRP ...
- 10、mysql增量恢复实战
10.1.实验数据: mysql> select * from test; +----+------+------+ | id | name | age | +----+------+----- ...
- promise的基本使用
// 什么情况下适用promise? // 一般情况下是有异步请求操作时,使用promise对这个异步操作进行封装 // new ->构造函数(1.保存了一些状态信息 2.执行传入的函数) // ...
- Go:go程序报错Cannot run program "C:\Users\dell\AppData\Local\Temp\___go_build_hello_go.exe" (in directory "…………"):该版本的 %1 与你运行的 Windows 版本不兼容。
问题截图 在go语言编译的时候,如果只是单单编译一个文件的话,package必须是main,意味着是可以单独编译的. 解决办法 修改为 package main 就可以 再次运行就可以啦. 文章转载至 ...
- 网络编程+Python
一.网络编程(模块:socket,from socket import *): 1. 网络层的IP地址可以唯一标识网络中的主机,传输层的"协议+端口"则可以唯一标识主机中应用程序( ...
- CF1539C Stable Groups[题解]
Stable Groups 题目大意 给定 \(n\) 个数 \(a_i\) ,你可以将这些数分成若干组,但每一组的元素满足按升序排列后,相邻两个元素值的差不超过 \(x\) .在分组前,你可以向这些 ...