js数据类型
js数据类型分为基本数据类型
和引用数据类型
基本数据类型(值类型):
Number
数字
String
字符串
Boolean
布尔
Null
空
Undefined
未定义
Symbol
符号
注:Symbol 是 ES6 引入了一种新的原始数据类型,表示独一无二的值。
引用数据类型:
Object
对象
Array
数组
Function
函数
判断数据类型的几种方法
1.最常见的判断方法:typeof
2.已知对象类型: instanceof
3.根据对象的构造器 constructor
进行判断
4.对象原型链判断方法:prototype
通用但很繁琐
5.jQuery方法:jquery.type()
一、typeof
其中typeof返回的类型都是字符串形式,需注意!
alert(typeof "helloworld") ------------------>"string"
alert(typeof 123) ------------------>"number"
alert(typeof [1,2,3]) ------------------>"object"
alert(typeof new Function()) ------------------>"function"
alert(typeof new Date()) ------------------>"object"
alert(typeof new RegExp()) ------------------>"object"
alert(typeof Symbol()) ------------------>"symbol"
alert(typeof true) ------------------>"true"
alert(typeof null) ------------------>"object"
alert(typeof undefined) ------------------>"undefined"
alert(typeof 'undefined') ------------------>"string"
二、instance of
注意: instanceof
后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。
[1,2,3] instanceof Array -------->true
new Date() instanceof Date -------->true
new Function() instanceof Function -------->true
new Function() instanceof function -------->false
null instanceof Object -------->false
三、根据对象的constructor进行判断
constructor
判断方法跟 instanceof
相似,但是 constructor
检测 Object
与 instanceof
不一样,constructor
还可以处理基本数据类型的检测,不仅仅是对象类型。
注意:
-
null和undefined没有constructor
-
判断数字时使用(),比如 (123).constructor,如果写成123.constructor会报错
-
constructor在类继承时会出错,因为Object被覆盖掉了,检测结果就不对了
//注意当出现继承的时候,使用constructor会出现问题
function A() {};
function B() {};
A.prototype = new B(); //A继承自B
console.log(A.constructor === B) -------->false
var C = new A();
//现在开始判断C是否跟A的构造器一样
console.log(C.constructor === B) -------->true
console.log(C.constructor === A) -------->false
//解决这种情况,通常是手动调整对象的constructor指向
C.constructor = A; //将自己的类赋值给对象的constructor属性
console.log(C.constructor === A); -------->true
console.log(C.constructor === B); -------->false
四、对象原型链判断方法: Object.prototype.toString.call()
适用于所有类型的判断检测,注意区分大小写 .toString
方法,在 Object
原型上返回数据格式。
console.log(Object.prototype.toString.call("123")) -------->[object String]
console.log(Object.prototype.toString.call(123)) -------->[object Number]
console.log(Object.prototype.toString.call(true)) -------->[object Boolean]
console.log(Object.prototype.toString.call([1, 2, 3])) -------->[object Array]
console.log(Object.prototype.toString.call(null)) -------->[object Null]
console.log(Object.prototype.toString.call(undefined)) -------->[object Undefined]
console.log(Object.prototype.toString.call({name: 'Hello'})) -------->[object Object]
console.log(Object.prototype.toString.call(function () {})) -------->[object Function]
console.log(Object.prototype.toString.call(new Date())) -------->[object Date]
console.log(Object.prototype.toString.call(/\d/)) -------->[object RegExp]
console.log(Object.prototype.toString.call(Symbol())) -------->[object Symbol]
五、jQuery方法: jquery.type()
据说是无敌万能的方法,如果对象是 null
跟 undefined
,直接返回 null
和 undefined
注意: 在使用时,一定要引入jquery文件,不然会报错
console.log(jQuery.type(undefined) === "undefined") -------->true
console.log(jQuery.type() === "undefined"); -------->true
console.log(jQuery.type(window.notDefined) === "undefined") -------->true
console.log(jQuery.type(123) === "number") -------->true
console.log(jQuery.type('123') === "string") -------->true
console.log(jQuery.type([]) === "array") -------->true
console.log(jQuery.type(true) === "boolean") -------->true
console.log(jQuery.type(function(){}) === "function") -------->true
console.log(jQuery.type(new Date()) === "date") -------->true
console.log(jQuery.type(/\d/) === "regexp") -------->true
console.log(jQuery.type(new Error()) === "error") -------->true jq版本高于1.9.3
console.log(jQuery.type({name:'Hello'}) === "object") -------->true
console.log(jQuery.type(Symbol()) === "symbol") -------->true
-------->其余对象类型一般返回object
总结:
一般变量用 typeof
已知对象类型用 instanceof
通用方法 Object.prototype.toString.call()
jQuery项目万能方法 jQuery.type()