ES6 —— 继承

简介: ES6 —— 继承

一、call() 方法的使用

  1. call():调用这个函数,并且修改函数运行时的 this 指向。

语法:fun.call(thisArg, arg1, arg2, ...)

  1. this.Arg:当前调用函数 this 的指向对象
  2. arg1, arg2:传递的其他参数
  //ES5 直接调用
  function fn(){
        console.log('I want to eat.')
        console.log(this) //this 指向 window
    }
    fn()

abe3745d81034ab8a54db3994a008dfd.png

  // ES6 call() 方法
  function fn(){
        console.log('I want to eat.')
        console.log(this)
    }
    fn.call()

7e5fd723a6a2418b9795ba6fc541af9c.png

  //ES6 的 call()
  // call 方法
    function fn(x, y){
        console.log('I want to eat.')
        console.log(this) //this 指向 o 这个对象
        console.log(x + y)
    }
    let o = {
        name: '不易'
    }
    // 2.call() 可以改变 fn 的 this 指向 此时这个函数的 this 就指向了o 这个对象
    fn.call(o, 1 , 2)

c959a0669f2e48fd9878129e96ea7202.png

二、借用构造函数继承父类型属性

  1. 核心原理:通过 call() 把父类型的 this 指向子类型的 this,这样就可以实现子类型继承父类型的属性。
  // 1.父构造函数
    function Father(uname, age){
        // this 指向父构造函数的对象实例
        this.uname = uname
        this.age = age
    }
    // 2.子构造函数
    function Son(uname, age, score){
        // this 指向子构造函数的对象实例
        Father.call(this, uname, age) //把父亲的 this 改成 孩子的 this
        this.score = score
    }
    let son = new Son('张三', 18, 100)
    console.log(son)

2a17394bd0964ca18063f69d624ed9db.png

三、利用原型对象继承方法

5dd6331f4f2f43d9adf28759286b3f32.png

  // 1.父构造函数
    function Father(uname, age){
        // this 指向父构造函数的对象实例
        this.uname = uname
        this.age = age
    }  
    Father.prototype.money = function(){
        console.log('2w one month')
    }
    // 2.子构造函数
    function Son(uname, age, score){
        // this 指向子构造函数的对象实例
        Father.call(this, uname, age) //把父亲的 this 改成 孩子的 this
        this.score = score
    }
    // Son.prototype = Father.prototype 这样直接赋值有问题 如果修改了子原型对象 父原型对象也会跟着变化
    // 如果利用对象的形式修改了原型对象 别忘了利用 constructor 指回原来的构造函数
    // 由于 Son.prototype = new Father() 实例对象覆盖了Son.prototype对象 所以需要加一个 constructor
    Son.prototype.constructor = Son
    Son.prototype = new Father()
    // 这个是子构造函数专门的方法
    Son.prototype.exam = function(){
        console.log('I need to exam')
    }
    let son = new Son('张三', 18, 100)
    console.log(son)
    console.log(Father.prototype)
    console.log(Son.prototype.constructor)

f70aaacfa72a4f9aa1d6c9c5d1add1c8.png

四、类的本质

  1. 类的本质是一个函数。

可以简单的理解为:类就是构造函数的另外一种写法。

  class Star{
    }
    console.log(typeof Star) //function
  1. 类有原型对象 prototype。类所有方法都定义在类的 prototype 属性上。
  class Star{
    }
    console.log(Star.prototype) 

d94d0c3ed7914a24bd30508ed7792313.png

  1. 类原型对象 prototype 也有 constructor 指向类本身。
  class Star{
    }
    console.log(Star.prototype.constructor) 

3b91cfe1386e47c2bb385c55221f09ee.png

  1. 类可以通过原型对象方法添加对象。
  class Star{
    }
    Star.prototype.sing = function(){
        console.log('无名的人')
    }
    let a = new Star('毛不易')
    console.log(a)

5bed39a1e76442199d1eeabed2dca766.png

类创建的实例对象有 __proto__ 原型指向类的原型对象。

  class Star{
    }
    Star.prototype.sing = function(){
        console.log('无名的人')
    }
    let a = new Star('毛不易')
    console.log(a.__proto__ === Star.prototype) //true
  1. ES6 的类其实就是语法糖。

语法糖:一种便捷方法。简单理解,有两种方法可以实现同样的功能,但是一种写法更加清晰、方便,那这个方法就是语法糖。


相关文章
|
16天前
|
JavaScript 前端开发 Java
ES6新特性(六):类
ES6新特性(六):类
|
3月前
ES5、ES6类的定义
ES5和ES6都支持类的定义,但ES6引入了更简洁的语法。在ES5中,类是函数,方法绑定在原型上;而ES6使用`class`关键字,构造方法为`constructor`,方法直接定义在类内。ES6的类继承使用`extends`关键字,子类需调用`super`初始化父类属性。示例展示了Person类及其Student子类的定义和方法调用。
18 1
|
3月前
|
JavaScript 前端开发
ES6如何声明一个类?类如何继承?
ES6如何声明一个类?类如何继承?
32 0
|
9月前
ES5的继承和ES6的继承有什么区别
ES5的继承和ES6的继承有什么区别
45 0
|
12月前
ES5 / ES6 的继承除了写法以外还有什么区别
ES5 / ES6 的继承除了写法以外还有什么区别
|
Java 数据库连接
【ES系列四】——ESjdbc的封装
【ES系列四】——ESjdbc的封装
|
JavaScript
【ES6】类
【ES6】类
49 0
|
JavaScript 前端开发 Java
ES6_Extends如何对ES5的继承进行“糖化”
我用我拙劣的代码来将这层糖的成分细细分析一下。 下面一些方法,都是基于class被脱糖之后的分析。关于class是如何被脱糖的过程===>请先查看ES6-Class如何优雅的进行Prototype“糖化” 内容很精彩,但是不要忘记回来看这个。这个剧情也挺赤鸡的。