admin管理员组文章数量:1292076
I want to console the super
in my InheritComponent
constructor
method.
But in chrome console, it throw an Error. Why?
class BaseComponent extends React.Component{
static defaultProps = {
title: 'Learn React By Examples'
}
constructor(props) {
console.log(props);
super(props);
}
setTitle(title) {
document.title = title || this.props.title;
return document.title;
}
}
class InheritComponent extends BaseComponent{
state = {
title: ''
};
constructor(props) {
super(props);
//here throw an Error. Why? I want to console.log `super`
console.log(super);
}
ponentDidMount() {
const title = this.setTitle('组件继承')
this.setState({title});
}
render() {
return <div>
<p>I inherit BaseComponent</p>
<p>current title is {this.state.title}</p>
</div>
}
}
ReactDOM.render(
<InheritComponent />,
document.body
)
<script src=".1.0/react.min.js"></script>
<script src=".1.0/react-dom.min.js"></script>
I want to console the super
in my InheritComponent
constructor
method.
But in chrome console, it throw an Error. Why?
class BaseComponent extends React.Component{
static defaultProps = {
title: 'Learn React By Examples'
}
constructor(props) {
console.log(props);
super(props);
}
setTitle(title) {
document.title = title || this.props.title;
return document.title;
}
}
class InheritComponent extends BaseComponent{
state = {
title: ''
};
constructor(props) {
super(props);
//here throw an Error. Why? I want to console.log `super`
console.log(super);
}
ponentDidMount() {
const title = this.setTitle('组件继承')
this.setState({title});
}
render() {
return <div>
<p>I inherit BaseComponent</p>
<p>current title is {this.state.title}</p>
</div>
}
}
ReactDOM.render(
<InheritComponent />,
document.body
)
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react-dom.min.js"></script>
Above is my demo code.
Share Improve this question asked Aug 15, 2016 at 6:22 Lin DuLin Du 103k135 gold badges332 silver badges564 bronze badges 2- i think you cant add anything before calling super, can you remove your console.log statement and try once? – Harkirat Saluja Commented Aug 15, 2016 at 7:47
-
@HarkiratSaluja remove the console.log statement will work. But I want to console the
super
. And, my console.log statement is after thesuper(props)
statement – Lin Du Commented Aug 15, 2016 at 8:31
2 Answers
Reset to default 9 +50Reason is simple: super
is a keyword, not a function nor any variable. You cannot log super
the same as you cannot log var
or new
keywords.
If you want to log the constructor of your parent, you can try:
console.log(super.constructor);
In fact, super()
is just shorthand for super.constructor()
.
See more: https://developer.mozilla/pl/docs/Web/JavaScript/Reference/Operators/super
super
is a keyword, you can't use it like a variable. The only allowed ways to use super are outlined in the MDN documentation for it:
super([arguments]); // calls the parent constructor.
super.functionOnParent([arguments]);
If you want to print the parent class, use
console.log(super.constructor)
instead.
本文标签: javascriptWhy consolelog(super) in React Component constructor throw an ErrorStack Overflow
版权声明:本文标题:javascript - Why console.log(super) in React Component constructor throw an Error? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741548138a2384717.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论