您的位置:首页 > Web前端 > JavaScript

JavaScript中判断数据类型的四种方法

2017-12-03 00:33 866 查看

JavaScript中判断数据类型的四种方法

1.typeof操作符

返回值:

“undefined”——未定义;

“boolean”——布尔值;

“string”——字符串;

“number”——数值;

“object”——对象或 null(null是一个空的对象引用); 数组也会返回object。

“function”——函数。

“undefined”–undefined

typeof存在的问题:对于数组也会返回object。

var arr = ["Alex","Oliver"];
console.log(typeof arr);   //object


更多实例

console.log(typeof undefined)//'undefined'
console.log(typeof unknownVariable) //'undefined'
console.log(typeof null) // 'object'
console.log(typeof true) //'boolean'
console.log(typeof "abc")  //'string'
console.log(typeof 123)  //'number'
console.log(typeof function(){})  //'function'
console.log(typeof [])//'object'


jQuery中的$.type()方法解决了JavaScript中typeof操作符对数组的检测返回”object”的不足。

$.typeof()方法也是返回要检测的变量其数据类型的字符串形式:

console.log($.type(null));                        //"null"
console.log($.type(undefined));                      //"undefined"
console.log($.type(true));                        //"boolean"
console.log($.type(3) === "number");              //true
console.log($.type("字符串") === "string");        //true
console.log($.type({}) === "object");              //true
console.log($.type(function(){}) === "function");  //true
console.log($.type([]) === "array");               //true
console.log($.type(new Date()) === "date");        //true
console.log($.type(/\w/) === "regexp");           //true


2.instanceof操作符

检测变量的类型是字符串、数值、、布尔值、undefined、null还是对象采用typeof。

要检测某个变量是是哪种引用类型,采用instanceof操作符。

var person = {};
var colors = [];
var fn = function(){};
var pattern = /\w/;
console.log(person instanceof Object);   //true
console.log(colors instanceof Object);   //true
console.log(colors instanceof Array);    //true
console.log(fn instanceof Object);       //true
console.log(fn instanceof Function);     //true
console.log(pattern instanceof Object); //true
console.log(pattern instanceof RegExp); //true

//instanceof 不能用来判断基本类型的值
var str = "指南";
console.log(str instanceof String);      //false
console.log(str instanceof Object);      //false


所有引用类型的值都是Object的实例。使用instanceof检测引用类型值是否为Object时都会返回true;检测基本类型的值会返回false

3.toString()方法

使用Object.prototype上的原生toString()方法判断数据类型。

toString()是定义在Object.prototype上的。通过call指定参数为Object.prototype对象中的toString方法的上下文。

语法:

Object.prototype.toString.call(value)


使用apply()也可以

Object.prototype.toString.apply(value)


还可以省略Object.prototype.

toString.call(value)


(1)判断基本类型

console.log(toString.call(123))           //[object Number]
console.log(toString.call('123'))         //[object String]
console.log(toString.call(true))          //[object Boolean]
console.log(toString.call(null))          //[object Null]
console.log(toString.call(undefined))    //[object Undefined]


(2)判断原生引用类型

console.log(toString.call({}));               //[object Object]
console.log(toString.call([]));               //[object Array]
console.log(toString.call(function(){}));   //[object Function]
console.log(toString.call(/\w/));            //[object RegExp]
console.log(toString.call(new Date()));     //[object Date]


4.constructor 属性

对象的constructor属性指向其构造函数。

var str = "字符串";
var num = 123;
var bool = true;
var person = {};
var colors = [];
var fn = function(){};
var pattern = /\w/;
var date = new Date();
console.log(str.constructor === String);       //true
console.log(num.constructor === Number);       //true
console.log(bool.constructor === Boolean);      //true
console.log(person.constructor === Object);     //true
console.log(colors.constructor === Array);      //true
console.log(fn.constructor === Function);      //true
console.log(pattern.constructor === RegExp);   //true
console.log(date.constructor === Date);        //true
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: