JS重点知识

JS是前端的面试重点,以下记录着一些js的基础且重要的知识点


一、JS的数据类型

JS有两种数据类型,分别是基本数据类型和引用数据类型,基本数据类型有Number、String、Boolean、Undefined、Null以及es6新增的Symbol(表示独一无二的值),而引用数据类型统称为Object对象,包含对象、数组、函数、正则表达式。

  • 基本数据类型和引用数据类型的区别:

基本数据类型存储在栈中,而引用数据类型存储在堆中,存储的是他的引用地址。

二、数据类型的判断

1.typeof

typeof返回一个表示数据类型的字符串,它可以返回string、number、undefined、symbol、boolean、function,但null、对象、数组的均返回object,无法区分。

        console.log(typeof 1); //number
        console.log(typeof 'a'); //string
        console.log(typeof function name() {}); //function
        console.log(typeof undefined); //undefined
        console.log(typeof true); //boolean
        console.log(typeof Symbol(1)); //symbol
        console.log(typeof null); //object
        console.log(typeof {}); //object
        console.log(typeof []); //object

2.instanceof

instanceof不能检测基本数据类型定义的数据

2-1 构造函数示例

instanceof运算符用于检测构造函数的prototype属性是否出现在某个实例对象的原型链上

object instanceof constructor

  • object 某个实例对象
  • constructor 某个构造函数
        //instanceof 来判断constructor.prototype是否存在于实例对象的原型链上
        class Employee {
            constructor(public fullName: string, public salary: number) {
                this.fullName = fullName;
                this.salary = salary;
            }
            getSalary() {
                return this.salary;
            }
        }
        const n = new Employee('zhangsan', 200);
        console.log(n instanceof Employee); //true
        console.log(n instanceof Object); //true

2-2 String对象和Date对象都属于Object对象和一些特殊情况

"instanceof" 表达式左侧必须是 "any" 类型、对象类型或类型参数。

instanceof不能检测undefined和null

 //下面的代码使用了 instanceof 来证明:String 和 Date 对象同时也属于Object 类型(他们是由 Object 类派生出来的)。
 let str = 'yi ge zi fu chuan';
        let newStr = new String('HAHAHA');
        let date = new Date();
        console.log(null instanceof Object);//报错
        console.log(undefined instanceof Object);//报错
        console.log(str instanceof String);//此时会报错 提示"instanceof" 表达式左侧必须是 "any" 类型、对象类型或类型参数。
        console.log(newStr instanceof String);//true
        console.log(newStr instanceof Object);//true
        console.log(date instanceof Date);//true
        console.log(date instanceof Object);//true
        console.log({} instanceof Object);//true

而 newStr instanceof String则返回true 因为newStr为实例对象

3、constructor

除了undefined和null,其余基本数据类型都是包装类对象,所以可以访问constructor。

constructor相比于instanceof的优点是可以检测基本数据类型,且不会沿着原型链查找

        let a = Number(1);
        let b = '2';
        let arr: any = [];
        console.log(a.constructor === Number);
        console.dir(b.constructor === String);
        console.dir(arr.constructor === Array);
        console.dir(arr.constructor === Object);

4、Object.prototype.toString.call()

Object.prototype.toString.call()是最准确的判定方式

Object.prototype.toString() 方法,会返回一个形如 [object type]的字符串。

        console.dir(Object.prototype.toString.call('1')); //[object String]
        console.dir(Object.prototype.toString.call(1)); // [object Number]
        console.dir(Object.prototype.toString.call([])); // [object Array]
        console.dir(Object.prototype.toString.call({})); // [object Object]
        console.dir(Object.prototype.toString.call(function name() {})); //[object Function]
        console.dir(Object.prototype.toString.call(new RegExp(''))); //[object RegExp]
        console.dir(Object.prototype.toString.call('1')); // [object String]

总结

以上四种都是判断数据类型的方法,此外在第三种方法中有一个包装类对象的概念可以看下这篇文章。http://t.csdn.cn/9BZWs

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值