zoukankan      html  css  js  c++  java
  • 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,在renderthis.props都是可以使用的,这是React自动附带的,是可以不写的:

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

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

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

    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过程,无论是否传入propsReact内部都会将porps赋值给组件实例porps属性中

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

    本文来自博客园,作者:喆星高照,转载请注明原文链接:https://www.cnblogs.com/houxianzhou/p/15047559.html

  • 相关阅读:
    C++ generic tools -- from C++ Standard Library
    18 12 18 给服务器添加logging 日志功能
    18 12 14 python提高 装饰器
    18 12 `12 WSGI 协议
    18 12 07 MySQL 与python 的交互
    转 SQL 的数据库 架构规范 之 58到家数据库30条军规解读
    18 12 06 sql 的 基本语句 查询 条件查询 逻辑运算符 模糊查询 范围查询 排序 聚合函数 分组 分页 连接查询 自关联 子查询
    18 12 4 SQL 的基本 语法
    clion 的 安装 变量配置的 搬运工(有点基础应该能看 大家看不懂 就是我自己看 哈哈哈哈哈哈)
    18 11 27 高级的服务器连接 epoll
  • 原文地址:https://www.cnblogs.com/houxianzhou/p/15047559.html
Copyright © 2011-2022 走看看