React(2) --super关键字
2024-09-05 23:50:41
参考:http://www.phonegap100.com/thread-4911-1-1.html
Es6中的super可以用在类的继承中,super关键字,它指代父类的实例(即父类的this对象)。子类必须在constructor方法中调用super方法,否则新建实例时会报错。这是因为子类没有自己的this对象,而是继承父类的this对象,然后对其进行加工。如果不调用super方法,子类就得不到this对象。
class Person {
constructor (name) {
this.name = name;
}
} class Student extends Person {
constructor (name, age) {
super(); // 用在构造函数中,必须在使用this之前调用
this.age = age;
}
}
为什么官方的列子里面写个super(props):
只有一个理由需要传递props作为super()的参数,那就是你需要在构造函数内使用this.props
那官方提供学习的例子中都是写成super(props),所以说写成super(props)是完全没问题的,也建议就直接这样写。
最新文章
- 【翻译】MongoDB指南/CRUD操作(一)
- POJ3666Making the Grade[DP 离散化 LIS相关]
- Vue 为什么在 HTML 中监听事件?
- HDU 5694---BD String
- [POJ3177]Redundant Paths(双连通图,割边,桥,重边)
- Redisql: the lightning fast data polyglot【翻译】 - Linvo's blog - 博客频道 - CSDN.NET
- Grunt使用教程(限winows)
- leetcode水题(一)
- J2EE规范标准
- ViewModel学习
- DS图遍历--广度优先搜索
- 解析Javascript事件冒泡机制(转) 本文转自:http://blog.csdn.net/luanlouis/article/details/23927347
- Lua入门教程
- 善用Eclipse的代码模板功能
- java 异常链
- 前端html页面学习---html部分
- Android实战——GreenDao3.2的使用,爱不释手
- Harbor快速部署到Kubernetes集群及登录问题解决
- hdu 5943(素数间隔+二分图匹配)
- 用Python实现BP神经网络(附代码)