Js获取数据类型
Js获取数据类型
JavaScript有着七种基本类型String、Number、Boolean、Null、Undefined、Symbol、Object,前六种为基本数据类型,Object为引用类型。
typeof
typeof(operand)或typeof operand,operand是一个表示对象或原始值的表达式,其类型将被返回。
规则
String:"string"Number:"number"Boolean:"boolean"Null:"object"Undefined:"undefined"Symbol:"symbol"BigInt:"bigint"Function Object:"function"Object:"Object"
示例
console.log(typeof(a)); // undefined // 未定义的变量
console.log(typeof("s")); // string
console.log(typeof(1)); // number
console.log(typeof(true)); // boolean
console.log(typeof(new String("s"))); // object
console.log(typeof(new Number(1))); // object
console.log(typeof(new Boolean(true))); // object
console.log(typeof(null)); // object // 在 JavaScript 最初的实现中,JavaScript 中的值是由一个表示类型的标签和实际数据值表示的。对象的类型标签是 0。由于 null 代表的是空指针(大多数平台下值为 0x00),因此,null 的类型标签是 0,typeof null 也因此返回 "object"。
console.log(typeof(undefined)); // undefined
console.log(typeof(Symbol())); // symbol
console.log(typeof(Object(Symbol()))); // object
console.log(typeof(1n)); // bigint // ES10(ES2019)新增基本数据类型
console.log(typeof(Object(BigInt(1n)))); // object
console.log(typeof(function() {})); // function
console.log(typeof([])); // object
console.log(typeof(new Date())); // object
console.log(typeof(/regex/)); // object
console.log(typeof({})); // object
instanceof
instanceof运算符用于检测构造函数的prototype属性是否出现在某个实例对象的原型链上。在Js中,一切都是对象,至少被视为一个对象,能够直接使用字面量声明的基本数据类型,虽然并不是直接的对象类型,但是在基本数据类型的变量调用方法的时候,会出现一个临时的包装对象,从而能够调用其构造函数的原型的方法,所以使用instanceof时对于字面量声明的String、Number、Boolean、Symbol、BigInt都会返回false。
示例
console.log("s" instanceof String); // false
console.log(1 instanceof Number); // false
console.log(true instanceof Boolean); // false
console.log(new String("s") instanceof String); // true
console.log(new Number(1) instanceof Number); // true
console.log(new Boolean(true) instanceof Boolean); // true
console.log(null instanceof Object); // false
console.log(undefined instanceof Object); // false
console.log(Symbol() instanceof Symbol); // false
console.log(Object(Symbol()) instanceof Symbol); // true
console.log(1n instanceof BigInt); // false
console.log(Object(1n) instanceof BigInt); // true
console.log(Symbol() instanceof Symbol); // false
console.log((function() {}) instanceof Function); // true
console.log([] instanceof Array); // true
console.log(new Date() instanceof Date); // true
console.log(/regex/ instanceof RegExp); // true
console.log({} instanceof Object); // true
Object.prototype.toString
借助Object原型的toString方法判断类型,一般对于新建的不同类型的对象toString方法都会被重新定义,无法沿着原型链到达Object.prototype.toString,可以通过call或者apply来调用Object.prototype.toString用以判断类型。
示例
console.log(Object.prototype.toString.call("s")); // [object String]
console.log(Object.prototype.toString.call(1)); // [object Number]
console.log(Object.prototype.toString.call(true)); // [object Boolean]
console.log(Object.prototype.toString.call(new String("s"))); // [object String]
console.log(Object.prototype.toString.call(new Number(1))); // [object Number]
console.log(Object.prototype.toString.call(new Boolean(true))); // [object Boolean]
console.log(Object.prototype.toString.call(null)); // [object Null]
console.log(Object.prototype.toString.call(undefined)); // [object Undefined]
console.log(Object.prototype.toString.call(Symbol())); // [object Symbol]
console.log(Object.prototype.toString.call(Object(Symbol()))); // [object Symbol]
console.log(Object.prototype.toString.call(1n)); // [object BigInt]
console.log(Object.prototype.toString.call(Object(BigInt(1n)))); // [object BigInt]
console.log(Object.prototype.toString.call(function() {})); // [object Function]
console.log(Object.prototype.toString.call([])); // [object Array]
console.log(Object.prototype.toString.call(new Date())); // [object Date]
console.log(Object.prototype.toString.call(/regex/)); // [object RegExp]
console.log(Object.prototype.toString.call({})); // [object Object]
每日一题
https://github.com/WindrunnerMax/EveryDay
参考
https://www.cnblogs.com/sban/p/10256412.html
https://www.cnblogs.com/yucheng6/p/9747313.html
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Data_structures
Js获取数据类型的更多相关文章
- js中获取数据类型
ES5中,js中数据类型:number.string.boolean.undefined.null.object js中获取数据类型常用的四种方式 实例: var a = 123, b = true, ...
- Js获取当前日期时间及其它操作
Js获取当前日期时间及其它操作var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); //获取完整的年份 ...
- js 获取系统当前时间
JS获取当前的日期和时间的方法:var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); //获取完整的年 ...
- JS中数据类型及原生对象简介
js是一种专门设计用来给网页增加交互性的编程语言,它的技术体系包含了一下几个方面: 1.JavaScript核心语言定义:包括数据类型,变量,常量,运算符,语句等. 2.原生对象和内置对象 3.浏览器 ...
- JS获取当前日期时间及JS日期格式化
Js获取当前日期时间: var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份( ...
- Js获取当前日期时间及时间相关操作
Js获取当前日期时间及时间格式 var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); ...
- js获取当前时间,js时间函数
Js获取当前日期时间及其它操作,js时间函数 var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); ...
- html中通过js获取接口JSON格式数据解析以及跨域问题
前言:本人自学前端开发,一直想研究下js获取接口数据在html的实现,顺利地找到了获取数据的方法,但是有部分接口在调用中出现无法展示数据.经查,发现时跨域的问题,花费了一通时间,随笔记录下过程,以方便 ...
- 【转】Js获取当前日期时间及其它操作
Js获取当前日期时间及其它操作 原文地址:http://www.cnblogs.com/carekee/articles/1678041.html var myDate = new Date();my ...
- JS基本数据类型&流程控制
JS基本数据类型 number ----- 数值 boolean ----- 布尔值 string ----- 字符串 [x,y] -------数组 undefined ...
随机推荐
- 【rt-thread】SConscript文件添加格式必须是4空格开头
SConscript文件添加格式必须是4空格开头,TAB或其他数量的空格均错误
- phpcms: Warning: "continue" targeting switch is equivalent to "break" 解决方案
Warning: "continue" targeting switch is equivalent to "break". Did you mean to u ...
- 数据-CRUD
CRUD Create 创建 Read 读取 Update 更新 Delete 删除 场景 只要存在数据结构概念,就必存在CRUD
- [转帖]SQL Server 中如何移动tempdb到新的位置
https://www.cnblogs.com/OpenCoder/p/10322904.html 操作步骤:1.检查tempdb的逻辑名字和它的存在位置.可以使用下面语句: SELECT name, ...
- [转帖]TiDB 内存控制文档
https://docs.pingcap.com/zh/tidb/stable/configure-memory-usage 目前 TiDB 已经能够做到追踪单条 SQL 查询过程中的内存使用情况,当 ...
- [转帖]tidb Modify Configuration Dynamically
https://docs.pingcap.com/tidb/v6.5/dynamic-config This document describes how to dynamically modify ...
- 【转帖】nginx变量使用方法详解-7
https://www.diewufeiyang.com/post/581.html 在 (一) 中我们提到过,Nginx 变量的值只有一种类型,那就是字符串,但是变量也有可能压根就不存在有意义的 ...
- [转帖]重置 VCSA 6.7 root密码和SSO密码
问题描述 1.用root用户登录 VMware vCenter Server Appliance虚拟机失败,无法登录 2.vCenter Server Appliance 6.7 U1的root帐户错 ...
- linux使用脚本给文件的最后一行不换行的方式插入一句话
处理一下 sed -i '$s/$/&,xxxx.com/' /deploy/mailfailstart
- Springboot 内嵌Tomcat 的http连接池与thread的关系
前言 最近看了很多tcp/ip 连接以及 IO相关的文章,但是依旧对数据库连接池等的部分不是很清楚, 所以这里仅是简单描述一下tomcat对应的http连接池数量的情况,不考虑与数据库的连接池的情况. ...