K
K
KasAskar2019-07-18 18:11:49
React
KasAskar, 2019-07-18 18:11:49

What is the first thing created in this js code??

class Application extends React.Component {
    constructor(props) {
      super(props);
      this.state = {isDangerAlertShowed: true}
      this.toggleDangerAlert = this.toggleDangerAlert.bind(this);
    }

    toggleDangerAlert() {
      this.setState(prevState => ({
        isDangerAlertShowed: !prevState.isDangerAlertShowed
      }));
    }

In this code, I don't understand this: this.toggleDangerAlert = this.toggleDangerAlert.bind(this);
After all, when this line is read, the toggleDangerAlert method has not yet been created, it is created after the constructor, while this.toggleDangerAlert = this.toggleDangerAlert.bind(this); if there is no toggleDangerAlert method yet ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-07-18
@KasAskar

The method is in the prototype.
The constructor is called when an instance is created.
When you create an instance, the prototype is already defined.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question