JS数据类型及检测方法

JS数据类型

基本数据类型

​Number,String,Boolean,null,undefined,symbol,bigint(后两个为ES6新增)

symbol的作用:给对象添加属性和方法 保证独一无二
bigint:超过最大安全整数之后JS就不能正确运算 此时我们就可以用BigInt()函数进行转换处理 BigInt类型不能跟其他类型做运算

引用数据类型:

​ object:普通对象,数组对象,正则对象,日期对象,Math数学函数对象。

记忆:USONB you are so niubility
u:undefined
s:string symbol
o:object
n:null number
b:boolean

两种数据存储方式:

  1. 基本数据类型是直接存储在栈中的简单数据段,占据空间小、大小固定,属于被频繁使用的数据。栈是存储基本类型值和执行代码的空间。
  2. 引用数据类型是存储在堆内存中,占据空间大、大小不固定。引用数据类型在栈中存储了指针,该指针指向堆中该实体的起始地址,当解释器寻找引用值时,会检索其在栈中的地址,取得地址后从堆中获得实体。

两种数据类型的区别:

  1. 堆比栈空间大,栈比堆运行速度快。
  2. 堆内存是无序存储,可以根据引用直接获取。
  3. 基础数据类型比较稳定,而且相对来说占用的内存小。
  4. 引用数据类型大小是动态的,而且是无限的。

检测方法

typeof

可以检测出的数据类型为:string、number、function、boolean、symbol、bigint、undefined 即除了null以外的基本数据类型和function

typeof null为object 为什么?

null 有属于自己的类型 Null,而不属于Object类型,typeof 之所以会判定为 Object 类型,是因为JavaScript 数据类型在底层都是以二进制的形式表示的,二进制的前三位为 0 会被 typeof 判断为对象类型,而 null 的二进制位恰好都是 0 ,因此,null 被误判断为 Object 类型。

instanceof

A instanceof B用来判断A 是否为 B 的实例
原理:A是实例对象 有隐式原型属性__proto__ A.proto__指向一个对象 这个对象又有隐式原型属性__proto 该对象.proto__又指向一个对象 这个对象又有隐式原型属性__proto …… 如此形成原型链 原型链的尽头是Object的原型对象 这是条隐式原型链 B是(构造)函数 有显示原型属性prototype 若B.prototype所指向的对象是上述原型链中的某一个对象 则说明A是B的实例 返回true

手动实现 function isInstanceOf (child, Parent)

function new_instance_of(leftVaule, rightVaule) {
    let rightProto = rightVaule.prototype; // 取右表达式的 prototype 值
    leftVaule = leftVaule.__proto__; // 取左表达式的__proto__值
    while (true) {
        if (leftVaule === null) {
            return false;   
        }
        if (leftVaule === rightProto) {
            return true;   
        }
        leftVaule = leftVaule.__proto__
    }
}

constructor

constructor 为实例原型上的方法,指向他的构造函数。
在这里插入图片描述
null 和 undefined 是无效的对象,因此是不会有 constructor 存在的

toString()

toString()是 Object 的原型方法,调用该方法Object.prototype.toString(),默认返回当前对象的 [[Class]] 。这是一个内部属性,其格式为 [object Xxx] ,其中 Xxx 就是对象的类型。对于 Object 对象,直接调用 toString() 就能返回 [object Object] 。而对于其他对象,则需要通过 call / apply 来调用才能返回正确的类型信息。
在这里插入图片描述

let a = "string";
let b = 111;
let c = {};
let d = [1, 2, 3];
let e = function () {
    console.log("eee");
}
let f = undefined;
let g = null;
let h = new Date();
let i = /test/;
let j = true;

console.log(Object.prototype.toString.call(a) === '[object String]');//true
console.log(Object.prototype.toString.call(b) === '[object Number]');//true
console.log(Object.prototype.toString.call(c) === '[object Object]');//true
console.log(Object.prototype.toString.call(d) === '[object Array]');//true
console.log(Object.prototype.toString.call(e) === '[object Function]');//true
console.log(Object.prototype.toString.call(f) === '[object Undefined]');//true
console.log(Object.prototype.toString.call(g) === '[object Null]');//true
console.log(Object.prototype.toString.call(h) === '[object Date]');//true
console.log(Object.prototype.toString.call(i) === '[object RegExp]');//true

console.log(Object.prototype.toString.call(c) === '[object Object]');//true
console.log(Object.prototype.toString.call(d) === '[object Object]');//false
console.log(Object.prototype.toString.call(e) === '[object Object]');//false
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值