• JavaScript基础知识: 原型和原型链


    一、原型和原型链

    1.1 继承

    1. extends
    2. super
    3. 扩展或重写方法
    class People {
      constructor(name) {
        this.name = name
      }
    
      eat() {
        console.log(`${this.name} eat sth`)
      }
    }
    
    class Student extends People {
      constructor(name, number) {
        // super:把名字给父类People,父类会帮我们处理,调用父类构造函数,但是学号number是学生特有的,自己处理
        super(name)
        this.number = number
      }
      sayHi() {
        console.log(`姓名: ${this.name}  学号: ${this.number}`)
      }
    }
    const xialuo = new Student('夏洛', 10010)
    xialuo.sayHi()
    xialuo.eat() // 继承父类的可以直接调用
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    在这里插入图片描述

    class People {
      constructor(name) {
        this.name = name
      }
    
      eat() {
        console.log(`${this.name} eat sth`)
      }
    }
    
    class Student extends People {
      constructor(name, number) {
        // super:把名字给父类People,父类会帮我们处理,调用父类构造函数,但是学号number是学生特有的,自己处理
        super(name)
        this.number = number
      }
      sayHi() {
        console.log(`姓名: ${this.name}  学号: ${this.number}`)
      }
    }
    
    class Teacher extends People {
      constructor(name, major) {
        // 用super的好处是直接调用父类构造函数,如果你也把this.name = name 写一遍,也对,但是万一父类这一句改了怎么办,所有子类都要改
        super(name)
        this.major = major
      }
      teach() {
        console.log(`姓名: ${this.name}  教课: ${this.major}`)
      }
    }
    
    const xialuo = new Student('夏洛', 10010)
    xialuo.sayHi()
    xialuo.eat() // 继承父类的可以直接调用
    
    const wang = new Teacher('王老师', '语文')
    wang.teach()
    wang.eat()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    在这里插入图片描述

    1.2 类型判断 - instanceof

    在这里插入图片描述

    console.log(typeof Student, typeof People) // function  function
    
    • 1

    class 只是一个语法糖,并不是真正意义上C++/Java的那种类,实际上就是函数。
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    Student.prototype 和 xialuo.__proto__指向同一块内存

    • 每个class都有显示原型prototype
    • 每个实例都有隐式原型__proto__
    • 每个实例的__proto__指向对应class的prototype

    1.3 基于原型的执行规则

    1. 获取xialuo.name 或者执行 xialuo.sayhi()的时候
    2. 先在自身属性和方法上找
    3. 如果找不到则自动去__proto__中查找

    在这里插入图片描述

    类中,构造函数的成员变量都是通过hasOwnProperty()能查找到的,类中的成员函数其实属于原型链中的,通过hasOwnProperty()是不能查找到的。最顶层的类是Object,只要不是Object,那么我们平时创建的类比如Student一定可以通过Student.prototype.__proto__找到Object,从而调用到Object.prototype 原型链上的方法。在这里插入图片描述

    在这里插入图片描述
    class的原型本质:原型和原型链图示、属性和方法的执行规则
    二、手写简易jQuery考虑插件和扩展性

    class jQuery {
      constructor(selector) {
        const result = document.querySelectorAll(selector)
        const length = result.length
        for (let i = 0; i < length; i++) {
          this[i] = result[i]
        }
        this.length = length
      }
    
      get(index) {
        return this[index]
      }
    
      each(fn) {
        for (let i = 0; i < this.length; i++) {
          const elem = this[i]
          fn(elem)
        }
      }
    
      on(type, fn) {
        return this.each(elem => {
          elem.addEventListener(type, fn, false)
        })
      }
    }
    
    // 插件
    jQuery.prototype.dialog = function(txt) {
      alert(txt)
    }
    
    // 轮子,复用
    class myJQuery extends jQuery {
      selector(selector) {
        // super 调用父类的构造函数,真正的jQuery构造函数更复杂,所以,super直接调一下可以省很多代码
        super(selector)
      }
      // 扩展自己的方法...
      addClass(className) {}
      style(data) {}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
  • 相关阅读:
    提高生存能力的7个关键技巧!
    vue学习笔记——简单入门总结(一)
    重要的 SQL Server 函数 - 其他函数
    AWS SAP-C02 考试指南
    Flink - checkpoint Failure reason: Not all required tasks are currently running
    Shadowing Japanese (中上)Unit 3
    高级API之file类的使用
    电脑重装系统蓝屏是什么原因
    阿里巴巴面试题- - -JVM篇(二十一)
    Python中的多进程(一看就懂)
  • 原文地址:https://blog.csdn.net/GY_U_YG/article/details/124797483