判断 JS 的数据类型
判断 JS 数据类型,也就是我们说的类型检测,本文提供五种方法,分别是 typeof 运算符、instanceof 操作符、Object.prototype.toString 方法、constructor 属性、duck type
1、typeof 运算符
typeof 算是最常见的了,使用它会返回一个字符串,适合函数对象和基本类型(js中的基本类型:number、string、boolean、null、undefined、object[对象])的判断
3、Object.prototype.toString.call( ) 方法
var gettype = Object.prototype.toString;
gettype.call([]); // object Array
gettype.call(function(){}); // object Function
gettype.call(null); // object Null
gettype.call(''); // object String
gettype.call(undefined); // object Undefined
https://blog.csdn.net/yCharlee/article/details/52424603