L
L
lohmag2016-11-30 14:44:17
JavaScript
lohmag, 2016-11-30 14:44:17

Why is a JS class property overwritten?

why can the this.process property be overwritten in the GraphMaker constructor

for (let trunk in trunks) {
                if (trunks.hasOwnProperty(trunk)) {
                  let template = [];
                  process.name = trunk;
                  template.push({name: 'in', metric: trunks[trunk].input}, {name: 'out', metric: trunks[trunk].out});
                  let Graph = new GraphMaker($this.host);
                  Graph.updateDB(process, template);
                  Graph.render(process, '12h');
                }
              }

function GraphMaker(host) {
  this.host = host;
  const $this = this;
this.updateDB = function(process, metric) {
    this.process = process;
    this.metric = metric;
    let pathToDb = dbPath(process);
 console.log(this.process);
    fs.access(pathToDb, (err) => {
      console.log(this.process);
      if (err) {}
})

1 console.log
{ group: 'asterisk', name: 'E1-i3' }
{ group: 'asterisk', name: 'TEL-IP01' }
2 console.log
{ group: 'asterisk', name: 'TEL- IP01' }
{ group: 'asterisk', name: 'TEL-IP01'
} for each for pass, a separate instance is created by the constructor, but anyway, the variable inside is somehow overwritten and after performing the asynchronous action, the last value from the for iteration is already left, why?

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