ES6 —— 继承



@[TOC](文章目录)

---

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

> 语法:`fun.call(thisArg, arg1, arg2, ...)`
> 1. `this.Arg`:当前调用函数 this 的指向对象
> 2. `arg1, arg2`:传递的其他参数

```javascript
    //ES5 直接调用
    function fn(){
        console.log('I want to eat.')
        console.log(this) //this 指向 window
    }
    fn()
```
![在这里插入图片描述](https://img-blog.csdnimg.cn/abe3745d81034ab8a54db3994a008dfd.png)

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

    fn.call()
```
![在这里插入图片描述](https://img-blog.csdnimg.cn/7e5fd723a6a2418b9795ba6fc541af9c.png)

```javascript
    //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)
```
![在这里插入图片描述](https://img-blog.csdnimg.cn/c959a0669f2e48fd9878129e96ea7202.png)
#### 二、借用构造函数继承父类型属性
1. 核心原理:通过 `call()` 把父类型的 `this` 指向子类型的 `this`,这样就可以实现子类型继承父类型的属性。

```javascript
    // 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)
```
![在这里插入图片描述](https://img-blog.csdnimg.cn/2a17394bd0964ca18063f69d624ed9db.png)
#### 三、利用原型对象继承方法
![在这里插入图片描述](https://img-blog.csdnimg.cn/5dd6331f4f2f43d9adf28759286b3f32.png)

```javascript
    // 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)
```
![在这里插入图片描述](https://img-blog.csdnimg.cn/f70aaacfa72a4f9aa1d6c9c5d1add1c8.png)
#### 四、类的本质
1. 类的本质是一个函数。

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

```javascript
    class Star{

    }
    console.log(typeof Star) //function
```

2. 类有原型对象 `prototype`。类所有方法都定义在类的 `prototype` 属性上。

```javascript
    class Star{

    }
    console.log(Star.prototype) 
```
![在这里插入图片描述](https://img-blog.csdnimg.cn/d94d0c3ed7914a24bd30508ed7792313.png)

3. 类原型对象 prototype 也有 constructor 指向类本身。

```javascript
    class Star{

    }
    console.log(Star.prototype.constructor) 
```
![在这里插入图片描述](https://img-blog.csdnimg.cn/3b91cfe1386e47c2bb385c55221f09ee.png)

4. 类可以通过原型对象方法添加对象。

```javascript
    class Star{

    }
    Star.prototype.sing = function(){
        console.log('无名的人')
    }
    let a = new Star('毛不易')
    console.log(a)
```
![在这里插入图片描述](https://img-blog.csdnimg.cn/5bed39a1e76442199d1eeabed2dca766.png)

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

```javascript
    class Star{

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

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

#JavaScript#
全部评论

相关推荐

时间回到 8 个月前,经过 2 段实习后的我在 25 年初,背着八股文,投着 boss,offer 了几个几百人的小公司,由于家人的原因选择了一个沿海的制造业,以为这是一个最稳定舒服的选择,因为我不追求互联网的高薪,这个选择让我一直后悔到现在,这个 offer 提前实习是 4.5k,转正是 6.5k 包吃住,在前期春招时代,确实过的比较舒服,到了入职 2 个月后开始事情变的不对劲起来,开始写日报,周报,月报,日报没写截图发部门群里,扣 5 元,是不是很离谱,我们部门领导是个不懂业务也不懂技术的人,title 却是 it 架构师,我觉得我只是二本,可能春招过了不太好找了,忍忍,又过了 3 个月,事情开始更加离谱了,他居然还管我电脑屏幕摆放位置,然后他说需求,由于口音很重,我反问后他就会说,我说完了,我不管了,由于我是应届生,交了社保两个月也不好走就忍忍了,又过了 2 个月,软件研发计件!!!!修 bug 多少工时,写什么样的页面接口多少工时,工时是领导定的,现在取消了调休,强制加班,一个月前我面试了其他公司,国庆前 和我说 9 号给我发 offer ,可惜今天 9 号,她说,只要 985 了,鸽了我,也许待在压抑的环境,运气也变差,没关系,现在我决定离开这里,停止内耗!提出离职
第一份工作应该选高薪还是...
点赞 评论 收藏
分享
评论
1
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务