请选择 进入手机版 | 继续访问电脑版

UBAINS

 找回密码
 立即注册
搜索
热搜: UBAINS
查看: 3080|回复: 0

[学习笔记] JavaScript判断类型

[复制链接]

10

主题

11

帖子

447

积分

版主

Rank: 7Rank: 7Rank: 7

积分
447
QQ
发表于 2020-10-28 19:00:47 | 显示全部楼层 |阅读模式
本帖最后由 pidtfork 于 2020-10-28 19:02 编辑

JavaScript判断类型
编程中经常要用到判断类型,如回调时先判断入参是否为 function 对象,是的才执行和传参,否则的当作函数处理程序就报错了。


typeof运算符

这是平常使用最多的判断类型的运算符,但是这个有个非常不好的地方,typeof不能判断Array类型,只能判断到Object类型,这在有时候就被动了。

typeof运算符用于判断对象的类型,但是对于一些创建的对象,它们都会返回'object'

typeof 只返回以下6种类型,无法判断是否为数组类型

1.         'undefined'
2.         'boolean'
3.         'string'
4.         'number'
5.         'object'
6.         'function'



//数值
typeof 37 === 'number';
typeof 3.14 === 'number';
typeof(42) === 'number';
typeof Math.LN2 === 'number';
typeof Infinity === 'number';
typeof NaN === 'number'; // 尽管它是 "Not-A-Number" (非数值) 的缩写
typeof Number(1) === 'number'; // Number 会尝试把参数解析成数值

// 字符串
typeof '' === 'string';
typeof 'bla' === 'string';
typeof `template literal` === 'string';
typeof '1' === 'string'; // 注意内容为数字的字符串仍是字符串
typeof(typeof 1) === 'string'; // typeof 总是返回一个字符串
typeof String(1) === 'string'; // String 将任意值转换为字符串,比 toString 更安全

// 布尔值
typeof true === 'boolean';
typeof false === 'boolean';
typeof Boolean(1) === 'boolean'; // Boolean() 会基于参数是真值还是虚值进行转换
typeof !!(1) === 'boolean'; // 两次调用 ! (逻辑非) 操作符相当于 Boolean()

// Undefined
typeof undefined === 'undefined';
typeofdeclaredButUndefinedVariable === 'undefined';
typeofundeclaredVariable === 'undefined';

// 对象
typeof{a: 1} === 'object';
typeof new Date()=== 'object';
typeof /regex/ === 'object'; // 历史结果请参阅正则表达式部分

// 下面的例子令人迷惑,非常危险,没有用处。避免使用它们。
typeof new Boolean(true)=== 'object';
typeof new Number(1) === 'object';
typeof new String('abc') === 'object';

// 函数
typeof function(){} === 'function';
typeof Math.sin === 'function';
如果要判断是否为数组时
var value =[1, 2, 3]

Object.prototype.toString.call(value)               //[object Array]
Object.prototype.toString.call(value).slice(8, -1)  //Array
//slice(8, -1)截取第8位到最后1位

//写成一个判断数组类型的函数
function isArray(value){
    return Object.prototype.toString.call(value)=== "[object Array]";
}
//判断其他类型
function isNumber(value){ //是否 number
    return Object.prototype.toString.call(value)=== '[object Number]'
}
function isString(value){ //是否 string
    return Object.prototype.toString.call(value)=== '[object String]'
}
function isObject(value){ //是否 object
    return Object.prototype.toString.call(value)=== '[object Object]'
}
function isBoolean(value){ //是否 boolean
    return Object.prototype.toString.call(value)=== '[object Boolean]'
}
function isFunction(value){ //是否 function
    return Object.prototype.toString.call(value)=== '[object Function]'
}
function isDate(value){ //是否 date
    return Object.prototype.toString.call(value)=== '[object Date]'
}
function isUndefined(value){ //是否 undefined
    return Object.prototype.toString.call(value)=== '[object Undefined]'
}
function isNull(value){ //是否 null
    return Object.prototype.toString.call(value)=== '[object Null]'
}
function isRegExp(value){ //是否正则
    return Object.prototype.toString.call(value)=== '[object RegExp]'
}
function isError(value){ //是否错误对象
    return Object.prototype.toString.call(value)=== '[object Error]'
}



高级模式
B Color Image Link Quote Code Smilies |上传

本版积分规则

Archiver|手机版|小黑屋|BBS.UBAINSYUN.COM

GMT+8, 2024-3-28 17:44 , Processed in 0.052968 second(s), 19 queries .

Powered by UBAINS! X3.4

© 2001-2017 UBAINS Inc.

快速回复 返回顶部 返回列表