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 ...
随机推荐
- 为什么catch了异常,但事务还是回滚了?
前几天我发了这篇文章<我来出个题:这个事务会不会回滚?>得到了很多不错的反馈,也有不少读者通过微信.群或者邮件的方式,给了我一些关于test4的回复.其中还有直接发给我测试案例,来证明我的 ...
- 4、saltstack的使用
官方文档地址:http://repo.saltstack.com/#rhel 4.1.saltstatck介绍: 用户要一致,这里使用的是root用户: 用于批量管理成百上千的服务器: 并行的分发,使 ...
- 详解C++中的多态和虚函数
一.将子类赋值给父类 在C++中经常会出现数据类型的转换,比如 int-float等,这种转换的前提是编译器知道如何对数据进行取舍.类其实也是一种数据类型,也可以发生数据转换,但是这种转换只有在 子类 ...
- Redis 底层数据结构之String
文章参考:<Redis设计与实现>黄建宏 Redis 的 string 类型底层使用的是 SDS(动态字符串) 实现的, 具体数据结构如下: struct sdshdr { int len ...
- JS中的单例模式及单例模式原型类的实现
单例模式 单例模式的定义: 保证一个类只有一个实例,并提供一个访问它的全局访问点 通过一个简单的例子来了解单例模式的作用: class Div { constructor() { return doc ...
- 并发王者课-铂金10:能工巧匠-ThreadLocal如何为线程打造私有数据空间
欢迎来到<并发王者课>,本文是该系列文章中的第23篇,铂金中的第10篇. 说起ThreadLocal,相信你对它的名字一定不陌生.在并发编程中,它有着较高的出场率,并且也是面试中的高频面试 ...
- SpringBoot:SpringBoot项目中 HttpServletRequest ServletInputStream 读取不到文件数据流
在Springboot程序启动后,会默认添加OrderedCharacterEncodingFilter和HiddenHttpMethodFilter过滤器.在HiddenHttpMethodFilt ...
- STM32中的GPIO笔记
1.GPIO是STM32可控制的引脚,STM32的GPIO被分成很多组,每组有16个引脚.每个GPIO端口包含:2个32位配置寄存器(CRL.CRH),2个32位数据寄存器(IDR.ODR),1个32 ...
- python 分析文本文件
def count_words(filename):#统计指定文件单词的数量 """Count the approximate number of words in a ...
- 刚刚进公司不会SVN 菜鸟感觉好蛋疼-----------SVN学习记
这篇文章源于6月份给公司新人作的关于SVN使用的培训,转眼已经过了几个月的时间,丢了也怪可惜的,于是整理出来希望能够帮助后来人快速入门. 转载:https://blog.csdn.net/maplej ...