Ashun's 技術駅 Ashun's 技術駅
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • Vue
  • 现代web布局
  • React
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 技术资源
  • 第一阶段

    • HTML
  • 第二阶段

    • JavaScript
  • 第三阶段

    • Vue
  • 第四阶段

    • 实战项目
  • 每周测试

    • 每周
  • 其他

    • Vue引入UI框架
    • Web前端面试
    • Vue3-resource
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 福利资源
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Ashun

前端界的小学生
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • Vue
  • 现代web布局
  • React
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 技术资源
  • 第一阶段

    • HTML
  • 第二阶段

    • JavaScript
  • 第三阶段

    • Vue
  • 第四阶段

    • 实战项目
  • 每周测试

    • 每周
  • 其他

    • Vue引入UI框架
    • Web前端面试
    • Vue3-resource
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 福利资源
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • vue

  • vue3

  • es6

  • JavaScript

  • css

  • webpack

  • http

  • NodeJS

  • React

    • 01React
    • 02Real DOM_Virtual DOM
    • 03life cycle
    • 04state_props
    • 05super()_super(props)
      • 面试官:super() 和 super(props) 有什么区别?
      • 一、ES6 类
      • 二、类组件
      • 三、总结
      • 参考文献
    • 06setState
    • 07SyntheticEvent
    • 08Binding events
    • 09Building components
    • 10communication
    • 11key
    • 12React refs
    • 13class_function component
    • 14controlled_Uncontrolled
    • 15High order components
    • 16React Hooks
    • 17import css
    • 18animation
    • 19redux
    • 20Redux Middleware
    • 21how to use redux
    • 22React Router
    • 23React Router model
    • 24immutable
    • 25render
    • 26improve_render
    • 27diff
    • 28Fiber
    • 29JSX to DOM
    • 30Improve performance
    • 31capture error
    • 32server side rendering
    • 33summary
  • git

  • linux

  • typescript

  • algorithm

  • applet

  • design

  • 《Web前端面试》
  • React
xugaoyi
2022-03-25
目录

05super()_super(props)

# 面试官:super() 和 super(props) 有什么区别?

# 一、ES6 类

在 ES6 中,通过 extends 关键字实现类的继承,方式如下:

class sup {
  constructor(name) {
    this.name = name;
  }

  printName() {
    console.log(this.name);
  }
}

class sub extends sup {
  constructor(name, age) {
    super(name); // super代表的事父类的构造函数
    this.age = age;
  }

  printAge() {
    console.log(this.age);
  }
}

let jack = new sub("jack", 20);
jack.printName(); //输出 : jack
jack.printAge(); //输出 : 20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

在上面的例子中,可以看到通过 super 关键字实现调用父类,super 代替的是父类的构建函数,使用 super(name) 相当于调用 sup.prototype.constructor.call(this,name)

如果在子类中不使用 super,关键字,则会引发报错,如下:

报错的原因是 子类是没有自己的 this 对象的,它只能继承父类的 this 对象,然后对其进行加工

而 super() 就是将父类中的 this 对象继承给子类的,没有 super() 子类就得不到 this 对象

如果先调用 this,再初始化 super(),同样是禁止的行为

class sub extends sup {
  constructor(name, age) {
    this.age = age;
    super(name); // super代表的事父类的构造函数
  }
}
1
2
3
4
5
6

所以在子类 constructor 中,必须先代用 super 才能引用 this

# 二、类组件

在 React 中,类组件是基于 ES6 的规范实现的,继承 React.Component,因此如果用到 constructor 就必须写 super() 才初始化 this

这时候,在调用 super() 的时候,我们一般都需要传入 props 作为参数,如果不传进去,React 内部也会将其定义在组件实例中

// React 内部
const instance = new YourComponent(props);
instance.props = props;
1
2
3

所以无论有没有 constructor,在 render 中 this.props 都是可以使用的,这是 React 自动附带的,是可以不写的:

class HelloMessage extends React.Component {
  render() {
    return <div>nice to meet you! {this.props.name}</div>;
  }
}
1
2
3
4
5

但是也不建议使用 super() 代替 super(props)

因为在 React 会在类组件构造函数生成实例后再给 this.props 赋值,所以在不传递 props 在 super 的情况下,调用 this.props 为 undefined,如下情况:

class Button extends React.Component {
  constructor(props) {
    super(); // 没传入 props
    console.log(props);      //  {}
    console.log(this.props); //  undefined
    // ...
  }
}
1
2
3
4
5
6
7
8

而传入 props 的则都能正常访问,确保了 this.props 在构造函数执行完毕之前已被赋值,更符合逻辑,如下:

class Button extends React.Component {
  constructor(props) {
    super(props); // 没传入 props
    console.log(props);      //  {}
    console.log(this.props); //  {}
    // ...
  }
}
1
2
3
4
5
6
7
8

# 三、总结

在 React 中,类组件基于 ES6,所以在 constructor 中必须使用 super

在调用 super 过程,无论是否传入 props,React 内部都会将 porps 赋值给组件实例 porps 属性中

如果只调用了 super(),那么 this.props 在 super() 和构造函数结束之间仍是 undefined

# 参考文献

  • https://overreacted.io/zh-hans/why-do-we-write-super-props/ (opens new window)
  • https://segmentfault.com/q/1010000008340434 (opens new window)
编辑 (opens new window)
上次更新: 2023/08/06, 00:38:41
04state_props
06setState

← 04state_props 06setState→

最近更新
01
课件-react路由-V6
01-22
02
课件-国际化
01-22
03
课件-redux-toolkit
01-22
更多文章>
Theme by Vdoing | Copyright © 2019-2024 Evan Xu | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式