typeof 、Object.prototype.toString和 instanceof
数据类型
js 基本类型包括:Undefined symbol null string boolean number
js 引用类型包括:object array Date RegExp
typeof
我们一般用typeof来判断数据的类型的
接下来我们试试
console.log(typeof undefined) //undefined console.log(typeof Undefined) //undefined
console.log(typeof Null) //undefined
console.log(typeof null) //object
console.log(typeof '123') //string
console.log(typeof 123) //number
console.log(typeof true) //boolean
console.log(typeof {a:123}) //object
console.log(typeof (new Date)) //object
console.log(typeof function(){}) //function
console.log(typeof Infinity) //number
console.log(typeof NaN) //number
console.log(typeof Number(1)) //number
console.log(typeof Symbol('foo')) //symbol
console.log(typeof Symbol()) //symbol
console.log(typeof [1,2,3]) //object
</script>
typeof 是一个操作符,主要的目的是检测一个变量是不是基本数据类型的变量,同时也可以说是确定一个变量是字符串,数值,布尔值,还是undefined
的最佳工具。
上面很明显对于引用类型的判断只返回object,并不能反应是哪种数据类型
这样就引出了另一个判断数据类型的方法
Object.prototype.toString
console.log(Object.prototype.toString.call("jerry"));//[object String]
console.log(Object.prototype.toString.call(12));//[object Number]
console.log(Object.prototype.toString.call(true));//[object Boolean]
console.log(Object.prototype.toString.call(undefined));//[object Undefined]
console.log(Object.prototype.toString.call(null));//[object Null]
console.log(Object.prototype.toString.call({name: "jerry"}));//[object Object]
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(/\d/));//[object RegExp]
function Person(){};
console.log(Object.prototype.toString.call(new Person));//[object Object]
obj.toString()的结果和Object.prototype.toString.call(obj)的结果不一样,这是为什么?
这是因为toString为Object的原型方法,而Array 、Function等类型作为Object的实例,都重写了toString方法
instanceof
instanceof 就是判断一个实例是否属于某种类型

// 判断 foo 是否是 Foo 类的实例
function Foo(){}
var foo = new Foo();
console.log(foo instanceof Foo)//true
还有一些特殊的
console.log(Object instanceof Object);//true
console.log(Function instanceof Function);//true
console.log(Number instanceof Number);//false
console.log(String instanceof String);//false console.log(Function instanceof Object);//true console.log(Foo instanceof Function);//true
console.log(Foo instanceof Foo);//false
这个知识点就和原型链,具体请看
http://www.cnblogs.com/wangfupeng1988/p/3977987.html
typeof 、Object.prototype.toString和 instanceof的更多相关文章
- instanceof, typeof, & Object.prototype.toString
/** * * @authors Your Name (you@example.org) * @date 2016-11-18 09:31:23 * @version $Id$ */instanceo ...
- Object.prototype.toString & typeof
Object.prototype.toString & typeof Object.prototype.toString 获取某个对象属于哪种内置类型 typeof 得到某个对象的类型 差别 ...
- 类型判断----小白讲解typeof,instanceof,Object.prototype.toString.call()
1.typeof只能判断基本类型数据, 例子: typeof 1 // "number" typeof '1' // "string" typeof true ...
- JS四种判断数据类型的方法:typeof、instanceof、constructor、Object.prototype.toString.call()
1.typeof 1 console.log(typeof ""); //string 2 console.log(typeof 1); //number 3 console.lo ...
- JS基础-数据类型判断typeof、instanceof、Object.prototype.toString
typeof用在基本数据类型和函数时,返回其对应类型的描述,对于引用类型都返回为object. instanceof无法判断基本数据类型,对于引用类型数据,返回其其对应类型. Object.proto ...
- JavaScript instanceof深度剖析以及Object.prototype.toString.call()使用
本文由segementfalt上的一道instanceof题引出: var str = new String("hello world"); console.log(str ins ...
- 利用Object.prototype.toString方法,实现比typeof更准确的type校验
Object.prototype.toString方法返回对象的类型字符串,因此可以用来判断一个值的类型. 调用方法: Object.prototype.toString.call(value) 不同 ...
- Object.prototype.toString.call() 、 instanceof 以及 Array.isArray()判断数组的方法的优缺点
1. Object.prototype.toString.call() 每一个继承 Object 的对象都有 toString 方法,如果 toString 方法没有重写的话,会返回 [Object ...
- typeof 和 Object.prototype.toString.call 数据类型判断的区别
使用 typeof 来判断数据类型,只能区分基本类型,即 “number”,”string”,”undefined”,”boolean”,”object” 五种. 但 Object.prototype ...
随机推荐
- 2018-8-10-使用-IncrediBuild-提升-VisualStudio-编译速度
title author date CreateTime categories 使用 IncrediBuild 提升 VisualStudio 编译速度 lindexi 2018-08-10 19:1 ...
- tp5.0x代码执行
1.拿到站首先平复一下心情 看了一下robots.txt结构像dedecms,网站还存在CDN,日了狗看到这里其实都想放弃来着,懒癌晚期,然后接着使用云悉平台走了一波,看了一下得到真实IP,看来只给w ...
- lds 文件说明
主要符号说明 OUTPUT_FORMAT(bfdname) 指定输出可执行文件格式. OUTPUT_ARCH(bfdname) 指定输出可执行文件所运行 CPU 平台 ENTRY(symbol) 指定 ...
- OpenCV-Python Tutorials目录
版本 3.4.6 1 Introduction to OpenCV OpenCV介绍Learn how to setup OpenCV-Python on your computer! 2 Gui F ...
- (转)C++ 11 多线程--线程管理
说到多线程编程,那么就不得不提并行和并发,多线程是实现并发(并行)的一种手段.并行是指两个或多个独立的操作同时进行.注意这里是同时进行,区别于并发,在一个时间段内执行多个操作.在单核时代,多个线程是并 ...
- matlab 求已知概率密度函数的随机数生成
N=10000; %需要随机数的个数 a=zeros(N,1); %存放随机数的数列 n=0; f1=@(t) 1./(1.2*pi*(1+5*(t-7.3).^2)); f2=@(t) 1./(1. ...
- System.Web.Mvc.HttpGetAttribute.cs
ylbtech-System.Web.Mvc.HttpGetAttribute.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, P ...
- Vue学习笔记——Vue-router
转载:https://blog.csdn.net/guanxiaoyu002/article/details/81116616 第1节:Vue-router入门 .解读router/index.js文 ...
- java读写属性配置文件
package readproperties; import java.io.FileInputStream; import java.io.IOException; import java.io.I ...
- 安装MySql社区版(35-3)
1,https://dev.mysql.com/ --------------------------------------------------------------------------- ...