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
在上面的例子中,可以看到通过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代表的事父类的构造函数
}
}
所以在子类constructor
中,必须先调用super
才能引用this
二、类组件
在React
中,类组件是基于es6
的规范实现的,继承React.Component
,因此如果用到constructor
就必须写super()
才初始化this
这时候,在调用super()
的时候,我们一般都需要传入props
作为参数,如果不传进去,React
内部也会将其定义在组件实例中
// React 内部
const instance = new YourComponent(props);
instance.props = props;
所以无论有没有constructor
,在render
中this.props
都是可以使用的,这是React
自动附带的,是可以不写的:
class HelloMessage extends React.Component{
render (){
return (
<div>nice to meet you! {this.props.name}</div>
);
}
}
但是也不建议使用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
// ...
}
而传入props
的则都能正常访问,确保了 this.props
在构造函数执行完毕之前已被赋值,更符合逻辑,如下:
class Button extends React.Component {
constructor(props) {
super(props); // 没传入 props
console.log(props); // {}
console.log(this.props); // {}
// ...
}
三、总结
在React
中,类组件基于ES6
,所以在constructor
中必须使用super
在调用super
过程,无论是否传入props
,React
内部都会将porps
赋值给组件实例porps
属性中
如果只调用了super()
,那么this.props
在super()
和构造函数结束之间仍是undefined
super()和super(props)的更多相关文章
- react super() and super(props)
subclass: subclass is a class that extends another class. 即子类. In ES2015, to use 'this' in subclasse ...
- react super中的props
有的小伙伴每次写组件都会习惯性在constructor和super中写上props,那么这个是必要的吗?? 首先要明确很重要的一点就是: 可以不写constructor,一旦写了constructor ...
- 巩固java(四)----super和super()
引言: 一个公司里有普通员工和经理,他们之间有很多共同点,但也有一些差异,比如薪水问题,普通员工只有普通工资,经理在完成绩效后有一定比例的奖金.这时我们可以定义两个类Employee和Manager, ...
- Java学习——this、this()、super 和 super()的使用
编写程序:说明 this.super 和 super()的用法.程序首先定义 Point(点)类,然后创建点的子类 Line(线)),最后通过 LX7_3 类输出线段的长度. package Pack ...
- react中constructor和super()以及super(props)的区别。
react中这两个API出镜率超级高,但是一直不太懂这到底是干嘛的,有什么用:今天整理一下,方便自己查看同时方便大家. 1.constructor( )-----super( )的基本含义 const ...
- Python’s super() considered super!
如果你没有被Python的super()惊愕过,那么要么是你不了解它的威力,要么就是你不知道如何高效地使用它. 有许多介绍super()的文章,这一篇与其它文章的不同之处在于: 提供了实例 阐述了它的 ...
- super 多重继承 super() function with multilevel inheritance
Python | super() function with multilevel inheritance - GeeksforGeeks https://www.geeksforgeeks.org/ ...
- 如何理解react中的super() super(props)
class WebSite extends React.Component { constructor() { super(); this.state = { name: "菜鸟教程&quo ...
- react组件中的constructor和super小知识
react组件中的constructor和super小知识 1.react中用class申明的类一些小知识 如上图:类Child是通过class关键字申明,并且继承于类React. A.Child的类 ...
- class实现React继承以及constructor的super的问题
看这篇文章之前 建议先看看阮一峰 的Class继承 便于更好的理解 首先要知道一个问题 React的父子组件和组件类的继承有什么关系?答案是:没有关系 父子组件:指的得是组件标签包含关系 父子组件通过 ...
随机推荐
- 别名路径跳转 - vscode 插件
别名路径跳转 - vscode 插件
- 13 种在 JavaScript 中删除/过滤数组的方法【转】
英文 | https://javascript.plainenglish.io/13-methods-to-remove-filter-an-item-in-an-array-and-array-of ...
- SQL语句之基础增删改查
目录 插入:NSERT INTO 语句 删除:DELETE 语句 更新:UPDATE 语句 选择 SELECT 语句 SELECT DISTINCT 语句 相关子句.运算符.关键字 WHERE 子句 ...
- opus编解码的特色和优点
概念原理 Opus是一个有损音频压缩的数字音频编码格式,由Xiph.Org基金会开发,之后由互联网工程任务组(IETF)进行标准化,目标是希望用单一格式包含声音和语音,取代Speex和Vorbis ...
- MYSQL 是如何保证binlog 和redo log同时提交的?
MYSQL 一个事务在提交的时候能够保证binlog和redo log是同时提交的,并且能在宕机恢复后保持binlog 和redo log的一致性. 先来看看什么是redo log 和binlog,以 ...
- UDP可靠传输协议KCP的一些理解
UDP主要用在哪两个方面 游戏 音视频通话 为什么要使用UDP? 实时性的考虑,丢包重传,TCP协议栈重传无法控制,UDP重发可以自定义策略. 在DNS查询的时候,也使用UDP,对资源的考虑. 如何做 ...
- 从零开始写 Docker(七)---实现 mydocker commit 打包容器成镜像
本文为从零开始写 Docker 系列第七篇,实现类似 docker commit 的功能,把运行状态的容器存储成镜像保存下来. 完整代码见:https://github.com/lixd/mydock ...
- vue,vuex,element实现无限tab页效果
直接撸代码 ?满足你 码云地址 效果图 tab页由来 甲方爸爸的更改需求,无力反抗 分析代码 懒的写,直接撸就行 参考文章 点我
- 嵌入式C语言设计学习之C语言回顾
C的基本语法-回忆 1.C的结构 C语言的结构还是以函数为主体,通过其他资源的添加来实现高级语言逻辑.所有的操作都是基于主函数展开的.以主函数为顺序列表,其他函数作为功能模块,组成一个完整的系统.所以 ...
- 可变形卷积系列(三) Deformable Kernels,创意满满的可变形卷积核 | ICLR 2020
论文提出可变形卷积核(DK)来自适应有效感受域,每次进行卷积操作时都从原卷积中采样出新卷积,是一种新颖的可变形卷积的形式,从实验来看,是之前方法的一种有力的补充. 来源:晓飞的算法工程笔记 公众号 ...