T
T
t800zippygod2020-10-15 00:03:58
JavaScript
t800zippygod, 2020-10-15 00:03:58

What happens to the JS class object?

Good day to all!
There was such a serious question, I am doing a chain-chain in JS. From the first "add" method I return this, in the second I mutate the state of the objects field of this.
And so for some reason this field mutates even before the second method has been called.
I may be stupid somewhere, but I’ve already specifically driven, MB someone will help :\

add(data) {
    switch(data.kind) {
      case 'Placemark':
        this.object = placemark(data)
        break
    }

    console.log(this)
    console.log('in add')
    return this
  }

  setInto(clasterName) {
    console.log(this)
    console.log('i am here')
    if(! this.objects[clasterName]) {
      this.objects[clasterName] = []
    }
    this.objects[clasterName].push(this.object)
  }

These are the same two methods7
First, the mutated instance is displayed in the console, then "in add", confirming that we are still in the first method. Already then the logs are displayed from the second method. What is "magic"?

Call:
map.add({kind: 'Placemark'}).setInto('hrenSobachiy')

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question