javascript数据类型及判断

javascript数据类型:

基础数据类型

js数据分为两种类型:原始数据类型和引用数据类型。

原始数据类型有:string、number、boolean、undefined null Symbol(ES6新增)
引用数据类型有:Function、Object、Date、RegExp、Number、String、Boolean和自定义类等

基础数据类型判断

1、typeof()函数
对于原始数据类型,我们可以使用typeof()函数来判断他的数据类型:

typeof(1)                              //number
typeof("1")                            //string
typeof(true)                           //boolean
typeof(undefined)                      //undefined
typeof(null)                           //object
为什么 typeof 运算符对于 null 值会返回 "object"。
这实际上是 JavaScript 最初实现中的一个错误,然后被 ECMAScript 沿用了。
现在,null 被认为是对象的占位符,从而解释了这一矛盾,但从技术上来说,它仍然是原始值。

2、instanceof
typeof()函数对于原始类型的判断还差强人意,但他是没法用来区分引用数据类型的,因为所有的引用数据类型都会返回"object"。于是javascript引入了java中使用的instanceof,用来判断一个变量是否是某个对象的实例,所以对于引用类型我们使用instanceof来进行类型判断。

var obj = {};
obj instanceof Object;           //true

var arr = [];
arr instanceof Array;           //true

var now = new Date();
now instanceof Date;             //true

var func = function(){};
func instanceof Function;        //true

var str = "string";
str instanceof String;           //false

3、Object.prototype.toString.call()
在javascript高级程序设计中提供了另一种方法,可以通用的来判断原始数据类型和引用数据类型。
常用的类型判断函数:

	// 是否是字符串
    function isString(value){
        return Object.prototype.toString.call(value) == "[object String]";
    }
    // 是否是数字
    function isNumber(value){
        return Object.prototype.toString.call(value) == "[object Number]";
    }
    // 是否是布尔值
    function isBoolean(value){
        return Object.prototype.toString.call(value) == "[object Boolean]";
    }
    // 是否undefined
    function isUndefined(value){
        return Object.prototype.toString.call(value) == "[object Undefined]";
    }
    // 是否是null
    function isNull(value){
        return Object.prototype.toString.call(value) == "[object Null]";
    }
    // 是否数组
    function isArray(value){
        return Object.prototype.toString.call(value) == "[object Array]";
    }
    // 是否是函数
    function isFunction(value){
        return Object.prototype.toString.call(value) == "[object Function]";
    }
    // 是否是对象
    function isObject(value){
        return Object.prototype.toString.call(value) == "[object Object]";
    }
    // 是否是正则表达式
    function isRegExp(value){
        return Object.prototype.toString.call(value) == "[object RegExp]";
    }
    // 是否是日期对象
    function isDate(value){
        return Object.prototype.toString.call(value) == "[object Date]";
    }

4、constructor

在W3C定义中的定义:constructor 属性返回对创建此对象的数组函数的引用

就是返回对象相对应的构造函数。从定义上来说跟instanceof不太一致,但效果都是一样的

如: (a instanceof Array) //a是否Array的实例?true or false

(a.constructor == Array) // a实例所对应的构造函数是否为Array? true or false

举个例子:

function employee(name,job,born){
    this.name=name;
    this.job=job;
    this.born=born;
}

var bill=new employee("Bill Gates","Engineer",1985);
console.log(bill.constructor);
 //输出
 //function employee(name, jobtitle, born){
 //this.name = name; this.jobtitle = job; this.born = born;
 //}

那么判断各种类型的方法就是:

console.log([].constructor == Array);
console.log({}.constructor == Object);
console.log("string".constructor == String);
console.log((123).constructor == Number);
console.log(true.constructor == Boolean);

较为严谨并且通用的方法:

function isArray(object){
    return object && typeof object==='object' &&
            Array == object.constructor;
}

5、jquery判断js数据类型

$.isArray([1,2]);              // 数组
$.isFunction(function () { }); // 函数function
$.isEmptyObject(null);         // null,undefind
$.isXMLDoc();    // xml
typeof (2) === "number";    // 数字
typeof ("2") === "string";  // 字符串
typeof (true) === "boolean";// bool型
typeof (function () { }) === "function";// 函数function
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值