I'm going through Egghead's React tutorial and in one of the lessons I found the following class declaration:
class StopWatch extends React.Component {
state = {lapse: 0, running: false}
render() {
const {lapse, running} = this.state
const buttonStyles = {
border: '1px solid #ccc',
background: '#fff',
fontSize: '2em',
padding: 15,
margin: 5,
width: 200
}
return (
<div style={{textAlign: 'center'}}>
<label style={{fontSize: '5em', display: 'block'}}>
{lapse}ms
</label>
<button style={buttonStyles}>{running ? 'Stop' : 'Start'}</button>
<button style={buttonStyles}>Clear</button>
</div>
)
}
}
So looking at the code, I was just curious about that assignment on the top. I looked at the class and extends documentations on MDN and it doesn't say anything about allowing an assignment inside a class declaration.
Furthermore, I tried it on a sample code and it's throwing an error:
class Square {
constructor(prop1, prop2) {
this.prop1 = prop1
this.prop2 = prop2
}
}
class Polygon extends Square {
constructor(prop1, prop2, prop3) {
super(prop1, prop2)
}
prop2 = prop3
render() {
console.log(prop2)
}
}
So... why does it work?