• js之继承


    1、原型 prototype 和 proto

    • 每个对象都有一个__proto__属性,并且指向它的prototype原型对象
    • 每个构造函数都有一个prototype原型对象
    • prototype原型对象里的constructor指向构造函数本身
      在这里插入图片描述
      作用:实例对象的__proto__指向构造函数的prototype,从而实现继承。prototype对象相当于特定类型所有实例对象都可以访问的公共容器
      在这里插入图片描述

    示例

    function Person(nick, age){
        this.nick = nick;
        this.age = age;
    }
    Person.prototype.sayName = function(){
        console.log(this.nick);
    }
    
    var p1 = new Person('Byron', 20);
    
    var p2 = new Person('Casper', 25);
    
    p1.sayName()  // Byron
    
    p2.sayName()  // Casper
    
    p1.__proto__ === Person.prototype       //true
    
    p2.__proto__ === Person.prototype   //true
    
    p1.__proto__ === p2.__proto__           //true
    
    Person.prototype.constructor === Person  //true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    2、原型链

    在这里插入图片描述

    arr.__proto__ === Array.prototype
    true
    Array.prototype.__proto__ === Object.prototype
    true
    arr.__proto__.__proto__ === Object.prototype
    true
    
    // 原型链的终点
    Object.prototype.__proto__ === null
    true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    原型链如下:

    arr ---> Array.prototype ---> Object.prototype ---> null
    
    • 1

    这就是传说中的原型链,层层向上查找,最后还没有就返回undefined

    3、继承

    继承是指一个对象直接使用另外一个对象的属性方法

    function Person (name, age) {
        this.name = name
        this.age = age
    }
    
    // 方法定义在构造函数的原型上
    Person.prototype.getName = function () { console.log(this.name)}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    定义Teacher构造函数

    function Teacher (name, age, subject) {
        Person.call(this, name, age)
        this.subject = subject
    }
    
    • 1
    • 2
    • 3
    • 4

    原理

    Teacher.prototype = Object.create(Person.prototype)
    
    Teacher.prototype.constructor
    ƒ Person(name, age) {
        this.name = name
        this.age = age
    }
    
    ---
    
    Teacher.prototype.constructor = Teacher
    ƒ Teacher(name, age, subject) {
        Person.call(this, name, age)
        this.subject = subject
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    继承方法的最终方案

    Teacher.prototype = Object.create(Person.prototype)
    Teacher.prototype.constructor = Teacher
    
    • 1
    • 2

    4、hasOwnProperty

    在原型链上查询属性比较耗时,对性能有影响,试图访问不存在的属性时会遍历整个原型链。
    遍历对象属性时,每个可枚举的属性都会被枚举出来。 要检查是否具有自己定义的属性,而不是原型链上的属性,必须使用hasOwnProperty方法。
    hasOwnProperty 是 JavaScript 中唯一处理属性并且不会遍历原型链的方法。

  • 相关阅读:
    【Java】【PAT】Basic Level 1020 月饼
    OAuth2 完成用户登录【详解】(含码云 gitee 的实现范例)
    window电脑关闭docker中正在运行redis的 RDB 和AOF 持久化选项
    逻辑回归-关于WOE和IV的一些理解
    刷题笔记day03-链表
    C语言深入理解指针(非常详细)(二)
    研报精选230528
    【21天Python进阶学习挑战赛】[day8]操作MySQL和SqlServer
    OpenCV 02(色彩空间)
    springboot昆明学院档案管理系统 毕业设计-附源码311758
  • 原文地址:https://blog.csdn.net/yaya_xiaohui/article/details/136295793