reactnative的js里的super的作用

class Blink extends Component{
    constructor(props){
        super(props);
        this.state = {
            showText: true
        };
        setInterval(
            () => {
                this.setState(funName => {
                    return{
                        showText: !funName.showText
                    };
                });
            }, 1000
        );
    }
    render(){
        let display = this.state.showText ? this.props.text:' ';
        return(
            <Text>{display}</Text>
        );
    }
}

 

子类必须在constructor方法中调用super方法,否则新建实例时会报错。这是因为子类没有自己的this对象,而是继承父类的this对象,然后对其进行加工。如果不调用super方法,子类就得不到this对象。

posted @ 2017-07-19 15:17  嘆世殘者——華帥  阅读(346)  评论(0编辑  收藏  举报