我目前正在进行官方的ReactJS教程:
https://reactjs.org/tutorial/tutorial.html#passing-data-through-props
我已经喜欢上了ReactJS,尽管我肯定还没有完全理解其中的所有内容。其中之一就是“props”。我熟悉面向对象编程,知道什么是属性,也对继承有至少一些了解。然而,似乎在这里,“props”突然冒出来:
通常情况下,当我在JavaScript中尝试面向对象编程时,我会通过在实例化对象时将父对象放入参数中来实现继承,例如:
我从未使用“X extends Y”语法,该语法在构造函数中使用super,就像上面的教程代码。然而,在我理解了super()的作用(调用父类的构造函数)之后,我想知道子类构造函数参数中的“props”来自哪里?Oo
在整个教程代码中,从未实例化过任何类的对象,请参见此处:https://codepen.io/gaearon/pen/gWWZgR?editors=0010 因此,我想知道子类构造函数参数如何被填充?Oo
class Square extends React.Component {
constructor(props) {
super(props);
this.state = {
value: null,
};
}
render() {
return (
<button className="square" onClick={() => alert('click')}>
{this.props.value}
</button>
);
}
}
var ParentObjectInstance = new ParentObject()
var ChildObjectInstance = new ChildObject(ParentObjectInstance)
在整个教程代码中,从未实例化过任何类的对象,请参见此处:https://codepen.io/gaearon/pen/gWWZgR?editors=0010 因此,我想知道子类构造函数参数如何被填充?Oo