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 ...
随机推荐
- MapReduce1.0的缺陷
- OpenCV-Python Tutorials目录
版本 3.4.6 1 Introduction to OpenCV OpenCV介绍Learn how to setup OpenCV-Python on your computer! 2 Gui F ...
- RabbitMQ学习第一记:用java连接RabbitMQ
1.什么是RabbitMQ MQ(Message Queue):消息队列,是服务端设计的一个可以存储大量消息的队列,并提供客户端操作队列的方法:生产队列(向队列中添加数据).消费队列(从队列中取数据) ...
- Erlang学习记录:转义
转义 转义序列 含义 整数编码 \b 退格符 8 \d 删除符 127 \e 换码符 27 \f 换页符 12 \n 换行符 10 \r 回车符 13 \s 空格符 32 \t 制表符 9 \v 垂直 ...
- hdu多校第四场 1003 (hdu6616) Divide the Stones 机智题
题意: 给你重量分别为1到n的n个石头,让你分成重量相等,数量也相等的k组,保证k是n的约数.问你能不能分配,如果能,输出具体的分配方案. 题解: 首先,如果1到n之和不能整除k,那么一定不能如题意分 ...
- Visual Studio上开发Python六大功能
Visual Studio上开发Python六大功能 一.整合 Python 直译器 (Interpreter) & 互动视窗 (Interactive) Visual Studio 高度整合 ...
- Hadoop 平台搭建
一.在Linux中安装JDK并配置环境变量 输入javac 查看是否已安装java环境如果没有安装 sudo apt-get install openjdk-7-jdk再次检测 javac修改配置参数 ...
- 05_mybatis动态sql
1.sql片段 1.sql片段**** mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 2.需求 用户信息综合查询列表和用户信息查询列表总数这两个sta ...
- Spring JdbcTemplate详解(9)
JdbcTemplate简介 Spring对数据库的操作在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中. JdbcTempla ...
- Linux使用crontab定时执行Python脚本清理日志
Linux中,周期执行的任务一般由crond这个守护进程来处理.cron读取一个或多个配置文件,这些配置文件中包含了命令行及其调用时间.crond的配置文件称为"crontab", ...