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

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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值