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 ...
随机推荐
- 深入学习Netty(2)——传统NIO编程
前言 学习Netty编程,避免不了从了解Java 的NIO编程开始,这样才能通过比较让我们对Netty有更深的了解,才能知道Netty大大的好处.传统的NIO编程code起来比较麻烦,甚至有遗留Bug ...
- 通过busybox制作根文件系统详细过程
我在之前的uboot通过NFS挂载ubuntu根文件系统中也有实现过根文件系统的制作,那只是在ubuntu官网已经做好的根文件基础上安装一些自己所需的软解而已.而使用busybox制作根文件系统可以自 ...
- 第5章:资源编排(YAML)
5.1 编写YAML注意事项 YAML 是一种简洁的非标记语言. 语法格式: 缩进表示层级关系 不支持制表符"tab"缩进,使用空格缩进 通常开头缩进 2 个空格 字符后缩进 1 ...
- 登录华科校园网,我用Socket
登录华科校园网,我用Socket 导语: 找一个华科学生问一问,学校的网络怎么样?得到的大多数是负面回答.其实不论是从覆盖区域.网络稳定性.还是速度来说,华科做的都还是可以的(24:00断网除外).可 ...
- java实现遍历文件目录,根据文件最后的修改时间排序,并将文件全路径存入List集合
package com.ultra.aliyun.control.main; import java.io.File; import java.util.ArrayList; import java. ...
- gitlab用户,组,项目权限管控
前言:gitlab上的权限管控是非常重要的,尤其是很多研发人员开发一个项目.这个是我总结的权限管控. 1.这个是创建项目时开放权限设置 2.这个创建用户设置的权限 3.用户权限,5种类型用户是 ...
- ESP32低功耗模式
1.ESP32 系列芯片提供三种可配置的睡眠模式,针对这些睡眠模式,我们提供了了多种低功耗解决方案,用户可以结合具体需求选择睡眠模式并进行配置.三种睡眠模式如下: Modem-sleep 模式:CPU ...
- Java on Visual Studio Code的更新 – 2021年6月
Nick Zhu from Microsoft VS Code Java Team 大家好,欢迎来到 Visual Studio Code Java 更新的特别年中版.作为这篇文章的重点,我们将看看接 ...
- Github Copilot 结合python的使用
之前提交的github copilot技术预览版申请,今天收到准入邮件,于是安上试一试这个准备把我送去电子厂上班的copy a lot ? 官网及申请地址:https://copilot.github ...
- ti
一.选择题DCBCDCDACAACBBABACBDCBBDA二.简答题(每小题5分,共20分)1. 1)简洁紧凑,灵活方便2)运算符丰富3)数据类型丰富4)C语言是结构化语言5)语法限制较少,程序设计 ...