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 ...
随机推荐
- 携程二面:讲讲 MySQL 中的 WAL 策略和 CheckPoint 技术
前段时间我在准备暑期实习嘛,这是当时面携程的时候二面的一道问题,我一脸懵逼,赶紧道歉,不好意思不知道没了解过,面试官又解释说 redo log,我寻思着 redo log 我知道啊,WAL 是啥?给面 ...
- LevelDB学习笔记 (2): 整体概览与读写实现细节
1. leveldb整体介绍 首先leveldb的数据是存储在磁盘上的.采用LSM-Tree实现,LSM-Tree把对于磁盘的随机写操作转换成了顺序写操作.这是得益于此leveldb的写操作非常快,为 ...
- 13、win10系统远程桌面oracle修正问题
1.A电脑(Windows 10)远程连接B电脑(Widows Server 2016), 出现错误: 出现身份验证错误.要求的函数不受支持 远程计算机:xx.xx.xx.xx 这可能是由于CredS ...
- mui 移动端网页双击事件处理
使用场景:需要在动态生成li列表数据中添加双击事件 定义事件 : var date1=null; function listdb(myKey) { var date2 = new Date(); if ...
- 巧用SpringBoot扩展点EnvironmentPostProcessor
我们的项目是单体项目,使用的是springboot的框架,随着对接的外部服务越来越多,配置文件越来越臃肿..我们将对接的外部服务的代码单独抽离出来形成service依赖,之后以jar包的形式引入,这时 ...
- 不安装任何软件或脚本使用powershell快速计算文件的MD5/SHA1/SHA256等校验值
有的时候在检查升级过程中需要对xml文件进行签名,而xml文件中一般都需要包含安装包的SHA256值,这里分享一个使用PowerShell快速计算SHA256等其他值的方法. 一.在需要计算文件SHA ...
- Python使用笔记20--网络操作小练习
1 ''' 2 2.自己抓取qq群的接口,传入一个群号,然后把群成员的头像下载到本地,头像用群备注来命名,如果没有 3 群备注,那么取昵称. 4 ''' 5 import requests 6 imp ...
- AI 预测蛋白质结构「GitHub 热点速览 v.21.29」
作者:HelloGitHub-小鱼干 虽然 AI 领域藏龙卧虎,但是本周预测蛋白质结构的 alphafold 一开源出来就刷爆了朋友圈,虽然项目与我无关,但是看着科技进步能探寻到生命机理,吃瓜群众也有 ...
- 短信链接点击跳转到微信小程序
短信轰炸的时代,之前链接都是跳转到网页的,后来发现粘性不强,再次唤醒用户成本较高,但小程序的订阅功能,再次唤醒成本较低,还便于给用户通知结果.所以现在链接都改跳转到小程序了.废话不多说,现在就看看是如 ...
- 【LeetCode】27.移除元素
27.移除元素 知识点:数组:双指针:: 题目描述 给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度. 不要使用额外的数组空间,你必须 ...