What happens to the JS class object?

  • 0
    Good day everyone!
    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.
    So for some reason this field mutates even before the second method was called.
    I may be stupid somewhere, but I am already specifically driven, mb who 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
    The mutated instance is first displayed in the console, then "in add" confirming that we are still in the first method. Only then the logs from the second method are displayed. What kind of "magic"?

    Call:
    map.add({kind: 'Placemark'}).setInto('hrenSobachiy')
    JavaScript Anonymous, Jun 14, 2019

  • 0 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!