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 ...
随机推荐
- 在银行业中,BP是指什么?
基点 Basis Point(BP)债券和票据利率改变量的度量单位.一个基点等于0.01个百分点,即0.01%,因此,100个基点等于1%.[例]一浮动利率债券的利率可能比LIBOR高10个基点,10 ...
- npm install时出现error
今天启动vue前端时,发现依赖没了.于是乎cmd->npm install->更新了部分依赖后,出现error信息,提示更新依赖失败.很奇怪,原来这个项目都是好的,为啥突然更新下来依赖了呢 ...
- 解决pip安装过慢的问题
1.pip install scipy -i https://pypi.douban.com/simple/ 2.pip install --index https://pypi.mirrors.us ...
- Docker(二)安装及常用命令
1.安装 1.安装虚拟机VMWare 链接:https://pan.baidu.com/s/1Xl7ENUm2gapPOFs-iXHpRQ 提取码:eubm 2.下载centos,我下的是这个版本的 ...
- LA3905 Meteor
https://vjudge.net/problem/UVALive-3905 计算出每个点在相框中的时间段,扫描线做即可 关键在如何快速计算每个点在相框中的时间段.对每个点进行运动分解,进入的时间L ...
- [NOI OJ]6044:鸣人和佐助
6044:鸣人和佐助 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 佐助被大蛇丸诱骗走了,鸣人在多少时间内能追上他呢? 已知一张地图(以二维矩阵的形式表示) ...
- nginx 下开启pathinfo模式
前几天自己新弄了个服务器,nginx的环境, 看到thinkcmf的框架,下载下来准备研究下,安装完成后,发现 url 是 普通模式,然后我就按照那个手册去后台开启了pathinfo模式,这一改完蛋了 ...
- 通过游戏学python 3.6 第一季 第五章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释--简单账号密码登陆 可复制直接使用 娱乐 可封装 函数
#猜数字--核心代码--猜测次数--随机函数和屏蔽错误代码---优化代码及注释--账号密码登陆 #!usr/bin/env python #-*-coding:utf-8-*- #QQ12411129 ...
- Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---适配器模式之TurkeyAdapter[转]
适配器模式的主要意图是对现有类的接口进行转换,以满足目标类的需求.其次,可以给目标类的接口添加新的行为(主要指方法).这一点容易与装饰模式混淆.从意图方面来看,装饰模式不改变(通常指增加)接口中的行为 ...
- VMware 安装 ubuntu 后安装 VMWare tools
1.如果 VMware 的安装 VMWare tools 的菜单是灰色, 很可能原因是: 你的 cdrom 被占用着. 关闭系统, 编辑配置, 把cdrom 改为 自动检测. 即不要开始就加载一 ...