I'm halfway to understanding how this.timerId doesn't cause an error in the react docs.
class Clock extends React.Component {
constructor(props) {
super(props);
this.state = {date: new Date()};
}
componentDidMount() {
this.timerID = setInterval(
() => this.tick(),
1000
);
}
componentWillUnmount() {
clearInterval(this.timerID);
}
tick() { this.setState({ date: new Date() }); }
render() {
return (
<div>
<h1>Hello, world!</h1>
<h2>It is {this.state.date.toLocaleTimeString()}.</h2>
</div>
);
}
}
ReactDOM.render(
<Clock />,
document.getElementById('root')
);
I get that timerId along with setInterval come from Node. I thought node was just the runtime, does it come with other modules aside from Node Timers? Where is timerID initialized and or inherited from? Do all objects get a timerID when running on Node?
this.timerID in this case is created as a new property at the point that it is assigned a Value, as pointed out by @luk2302 in the comments.
Below shows another possible way you could refer to a property that wasn't created in that class directly, but in the parent class.
class SuperClass {
constructor() {
this.superProperty = "Hello I'm super!"
}
}
class SupportClass extends SuperClass{
constructor(example) {
super();
this.example = example;
}
supportMethod() {
console.log(this.example);
console.log(this.superProperty);
}
}
let supportClass = new SupportClass("HELLO");
supportClass.supportMethod();
this produces an output:
HELLO
Hello I'm super!