typeof操作符,返回数据类型Array.isArray()、Object.prototype.toString.call()
源地址https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/typeof
typeof操作符
// Numbers
typeof 37 === 'number';
typeof 3.14 === 'number';
typeof Math.LN2 === 'number';
typeof Infinity === 'number';
typeof NaN === 'number'; // 尽管NaN是"Not-A-Number"的缩写
typeof Number(1) === 'number'; // 但不要使用这种形式! // Strings
typeof "" === 'string';
typeof "bla" === 'string';
typeof (typeof 1) === 'string'; // typeof总是返回一个字符串
typeof String("abc") === 'string'; // 但不要使用这种形式! // Booleans
typeof true === 'boolean';
typeof false === 'boolean';
typeof Boolean(true) === 'boolean'; // 但不要使用这种形式! // Symbols
typeof Symbol() === 'symbol';
typeof Symbol('foo') === 'symbol';
typeof Symbol.iterator === 'symbol'; // Undefined
typeof undefined === 'undefined';
typeof declaredButUndefinedVariable === 'undefined';
typeof undeclaredVariable === 'undefined'; // Objects
typeof {a:1} === 'object'; // 使用Array.isArray 或者 Object.prototype.toString.call
// 区分数组,普通对象
typeof [1, 2, 4] === 'object'; typeof new Date() === 'object';
Array.isArray()判断是否为数组
var ar = [];
var result = Array.isArray(ar);
// Output: true var ar = new Array();
var result = Array.isArray(ar);
// Output: true var ar = [1, 2, 3];
var result = Array.isArray(ar);
// Output: true var result = Array.isArray("an array");
document.write(result);
// Output: false
Object.prototype.toString.call()精准判断数据类型
console.log(Object.prototype.toString.call(123)) //[object Number]
console.log(Object.prototype.toString.call('123')) //[object String]
console.log(Object.prototype.toString.call(undefined)) //[object Undefined]
console.log(Object.prototype.toString.call(true)) //[object Boolean]
console.log(Object.prototype.toString.call({})) //[object Object]
console.log(Object.prototype.toString.call([])) //[object Array]
console.log(Object.prototype.toString.call(function(){})) //[object Function]
typeof操作符,返回数据类型Array.isArray()、Object.prototype.toString.call()的更多相关文章
- JS四种判断数据类型的方法:typeof、instanceof、constructor、Object.prototype.toString.call()
1.typeof 1 console.log(typeof ""); //string 2 console.log(typeof 1); //number 3 console.lo ...
- 数据类型的判断 --Object.prototype.toString.call(obj)精准检测对象类型
数据类型的判断 typeof typeof返回一个表示数据类型的字符串,返回结果包括:number.boolean.string.symbol.object.undefined.function等7种 ...
- js精确判断数据类型为何用Object.prototype.toString.call()而不是Object.prototype.toString()
有何区别,为何一定要通过call. 我们知道call是用来改变函数作用域的,Object.prototype.toString.call在这儿也是用来改变作用域的. Object.prototype. ...
- typeof 、Object.prototype.toString和 instanceof
数据类型 js 基本类型包括:Undefined symbol null string boolean number js 引用类型包括:object array Date RegExp typeo ...
- 从toString()方法到Object.prototype.toString.call()方法
一.toString方法和Object.prototype.toSting.call()的区别 var arr=[1,2]; 直接对一个数组调用toString()方法, console.log(ar ...
- Object.prototype.toString.call() 、 instanceof 以及 Array.isArray()判断数组的方法的优缺点
1. Object.prototype.toString.call() 每一个继承 Object 的对象都有 toString 方法,如果 toString 方法没有重写的话,会返回 [Object ...
- 前端面试题1:Object.prototype.toString.call() 、instanceof 以及 Array.isArray()三种方法判别数组的优劣和区别
1. Object.prototype.toString.call() 每一个继承 Object 的对象都有 toString 方法,如果 toString 方法没有重写的话,会返回 [Object ...
- JS基础-数据类型判断typeof、instanceof、Object.prototype.toString
typeof用在基本数据类型和函数时,返回其对应类型的描述,对于引用类型都返回为object. instanceof无法判断基本数据类型,对于引用类型数据,返回其其对应类型. Object.proto ...
- typeof 和 Object.prototype.toString.call 数据类型判断的区别
使用 typeof 来判断数据类型,只能区分基本类型,即 “number”,”string”,”undefined”,”boolean”,”object” 五种. 但 Object.prototype ...
随机推荐
- react-native start停止在Loading dependency graph, done.
在试验的过程中. 发现运行 react-native start会卡住,停留在Loading dependency graph, done. 原因大概是之前运行过 react-native run-a ...
- HDU2896 病毒侵袭 AC自动机模板
各种MLE,这模板感觉有问题,next数组开128也会MLE,实际上可见字符为编号32~126,只用开100就行. #include <iostream> #include <cst ...
- Javascript实现多行字符串
打开百度首页,进入控制台的时候,我们在console控制台总可以看到一段文字: 这些文字是如何显示在控制台的呢?? Javascript中的函数被看作是一个对象拥有自己的方法,其中一个小方法fn.to ...
- angular依赖注入(2)——注入器的使用
一.显示注入器 injector = ReflectiveInjector.resolveAndCreate([Car, Engine, Tires]); let car = injector.get ...
- 使用Jedis操作Redis-使用Java语言在客户端操作---对Sorted-Sets的操作
//对Sorted-Sets操作 /** * Sorted-Sets和Sets类型极为相似,它们都是字符串的集合,都不允许重复的成员出现在一个Set中. * 它们之间的主要差别是Sorted-Sets ...
- 基础架构:一条SQL查询语句是如何执行的?
https://time.geekbang.org/column/article/68319?code=pEYaxHnjO23LQHW4CJgL706EXlpAJnbOOiT2y42cWwU%3D 这 ...
- python元祖和列表
下面讲到的分别有: 列表:元祖 列表的定义 list(列表)是python中使用最频繁的数据类型,在其他语言中叫做数组 专门储存一串信息 列表[ ]定义,数据之间有逗号分隔 列表的索引是从0开始的 ...
- NKOJ1469 通向自由的钥匙
P1469通向自由的钥匙 时间限制 : 10000 MS 空间限制 : 65536 KB 问题描述 通向自由的钥匙被放n个房间里,这n个房间由n-1条走廊连接.但是每个房间里都有特别 的保护魔 ...
- 【CodeVS】2750 心系南方灾区
2750 心系南方灾区 时间限制: 1 s 空间限制: 2000 KB 题目等级 : 青铜 Bronze 题目描述 Description 现在我国南方正在承受百年不遇的大雪.冻雨灾害.北京市已经开始 ...
- MongoDB 定位 oplog 必须全表扫描吗?
MongoDB oplog (类似于 MySQL binlog) 记录数据库的所有修改操作,除了用于主备同步:oplog 还能玩出很多花样,比如 全量备份 + 增量备份所有的 oplog,就能实现 M ...