JS数据类型 以及 常用类型判断

一、JS数据类型:
7个类型
number, string, object, function, Boolean, null, undefined

二、类型判断方式:
1. typeof

typeof operand
or
typeof (operand)

缺点:数组、null类型会被误判断为 Object形式

2. instanceof

object instanceof constructor
// object 要检测的对象 ; constructor 某个构造函数
// 举例:
[1, 2] instanceof Array
//  true

缺点:不能判断出数组不是对象类型, [] instanceof Object时会返回 true
产生原因:数组的proto最终指向Object.prototype。所以不能精确判断类型。
3. Object.toString.call()

object.toString()
// 返回 "[object type]",其中type是对象的类型

转载于:https://www.jianshu.com/p/57df42389621

猜你喜欢

转载自blog.csdn.net/weixin_33786077/article/details/91326462