js 判断数据类型

本文介绍了JavaScript ES6中新增的Symbol类型用于解决属性名冲突,以及BigInt提供大整数处理。同时讨论了typeof和instanceof的区别,以及如何通过constructor属性判断对象类型和自定义对象原型的注意事项。
摘要由CSDN通过智能技术生成
基本数据类型:string、boolean、number、nullundefined
引用数据类型:object (Data、function、Array、正则)
es6新增:
symbol: Symbol类型的对象永远不相等,即便创建的时候传入相同的值。
		因此,可以用解决属性名冲突的问题(适用于多少编码),做为标记。
BigInt:Javascript 中的任意精度整数,可以安全存储和操作大整数。
		即始超出 Number 能够表示的安全整数范围。是 chrome 67中的新功能。
  1. typeof

    只能返回7种数据类型:
    number、boolean、string、undefined、object、symbol、function
    typeof(null) 返回的是object
    引用类型:除了function返回的是function,其他都返回的是object
    null 有属于自己的数据类型 Null, 引用类型中的 数组、日期、正则 也都有属于自己的具体类型,而 typeof 对于这些类型的处理,只返回了处于其原型链最顶端的 Object 类型,没有错,但不是我们想要的结果。

    console.log(typeof (111)); // number
    console.log(typeof (true));// boolean
    console.log(typeof ('111'));// string
    console.log(typeof (null));// object
    console.log(typeof (undefined));// undefined
    
  2. instanceof

    A instanceof B  A是否是B的实例,如果是返回true 否则false
    检测的是原型
    

在这里插入图片描述
详情解释:
[ ]的原型指向Array.prototype,间接指向Object.prototype,因此
[ ] instanceof Array.prototype = true
[ ] instanceof Object.prototype = true
so,instanceof只能是判断两个对象是否属于实例关系,而不能判断一个对象具体属于哪个实例

  1. constructor

constructor 是原型prototype的一个属性,当函数被定义的时候,js引擎会为函数添加原型prototype,并且这个prototype中constructor属性指向函数引用,因此重写prototype会丢失原来的constructor。

问题
1:null 和 undefined 无constructor,这种方法判断不了。
2: 如果自定义对象,开发者重写prototype之后,原有的constructor会丢失。so,为了规范开发,在重写对象原型时一般都需要重新给constructor赋值,以保证对象实例的原型不会被篡改。

''.constructor == String  // true
new Number(1).constructor == Number // true
true.constructor == Boolean // true
new Function().constructor == Function // true
new Date().constructor == Date // true
[].constructor == Array // true
null.constructor == Null 
// Uncaught TypeError: Cannot read properties of null (reading 'constructor')
undefined.constructor == Undefined
// Uncaught TypeError: Cannot read properties of undefined (reading 'constructor')
  1. Object.prototype.toString.call

toString()是Object的原型方法,调用该方法,默认返回当前对象的[[Class]],这是一个内部属性,其格式为[object XXX],XXX就是返回的数据类型

Object.prototype.toString.call('') ;   // [object String]
Object.prototype.toString.call(1) ;    // [object Number]
Object.prototype.toString.call(true) ; // [object Boolean]
Object.prototype.toString.call(Symbol()); //[object Symbol]
Object.prototype.toString.call(undefined) ; // [object Undefined]
Object.prototype.toString.call(null) ; // [object Null]
Object.prototype.toString.call(new Function()) ; // [object Function]
Object.prototype.toString.call(new Date()) ; // [object Date]
Object.prototype.toString.call([]) ; // [object Array]
Object.prototype.toString.call(new RegExp()) ; // [object RegExp]
Object.prototype.toString.call(new Error()) ; // [object Error]
Object.prototype.toString.call(document) ; // [object HTMLDocument]
Object.prototype.toString.call(window) ; //[object global] window 是全局对象 global 的引用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值