This changing global var

0

I am making a change inside the main function array of the object, but this is changing several values, including variable outside the object, which can not happen. How can I fix this?

  var trackList [
    {id: 1, name: "Isometric (intro)", artist: "Madeon", currentDuration: 0, fullDuration: "5", …},
    {id: 2, name: "You're On", artist: "Madeon", currentDuration: 0, fullDuration: "15", …}
]

// Nesta parte alterto as informações com o this do trackObject.track
trackObject.prototype._nextTrack = function(newTrack){
  clearTimeout(this.loadingTimer);
    this._setTrackName.call(this, newTrack.name); // isto altera TODOS os valores do trackList[value].name dentro de var trackList
}
// Esta e uma orientação para alterar o array dentro do this.track.name em trackObject.
trackObject.prototype._setTrackName = function(name){
    this.track.name = name;
}
trackObject.prototype._callNextTrack = function(){
  trackCurrentListId = trackCurrentListId + 1;    
  if(trackCurrentListId >= trackList.length) trackCurrentListId = 0;

  var track = trackList[trackCurrentListId];
  this._nextTrack.call(this, track); // envio as novas informações de atualização do objeto
}

function trackObject(track){
    this.track = track;
    // track == {id: 1, name: "Isometric (intro)", artist: "Madeon", currentDuration: 0, fullDuration: "5", …}
    this._nextButton();
    this._updateTrack();
}

var track = {id: 1, name: "Isometric (intro)", artist: "Madeon", currentDuration: 0, fullDuration: "5", …}
new trackObject(track);
    
asked by anonymous 13.08.2017 / 17:00

1 answer

0
  

Warning : The comment space was small, so I had to add an answer.

How is this change occurring?

Your trackList variable is not set correctly, you set the object constructor at the end of the code (not that it makes a difference, but leaves everything more confusing), and did not instantiate your object correctly.

I do not know what you want to do properly, but start by organizing the code:

/**
    @description Construtor do objeto trackObject
**/
function trackObject( track ){
    this.track = track;
    this._nextButton();
    this._updateTrack();
}

/**
    @description Descreva o que o método faz aqui
    Adiciona novo método, _newTrack(), ao objeto
**/
trackObject.prototype._nextTrack = function ( newTrack ) {
    clearTimeout( this.loadingTimer );
    this._setTrackName.call( this, newTrack.name );
}

/**
    @description Descreva o que o método faz aqui
    Adiciona novo método, _setTrack(), ao objeto
**/
trackObject.prototype._setTrackName = function( name ) {
    this.track.name = name;
}

/**
    @description Descreva o que o método faz aqui
    Adiciona novo método, _callNextTrack(), ao objeto
**/
trackObject.prototype._callNextTrack = function() {
    trackCurrentListId = trackCurrentListId + 1;    
    if( trackCurrentListId >= trackList.length ) {
        trackCurrentListId = 0;
    }
    var track = trackList[trackCurrentListId];
    this._nextTrack.call( this, track );
}

// JSON válido. Ver JSONLint https://jsonlint.com/
var trackList = [
    {
        "id": 1,
        "name": "Isometric (intro)",
        "artist": "Madeon",
        "currentDuration": 0,
        "fullDuration": "5"
    },
    {
        "id": 2,
        "name": "You're On",
        "artist": "Madeon",
        "currentDuration": 0,
        "fullDuration": "15"
    }
];

var track = {
    id: 1,
    name: "Isometric (intro)",
    artist: "Madeon",
    currentDuration: 0,
    fullDuration: "5"
};

var minhaPlayList = new trackObject( track );

In addition, you need to better describe the issues that occur and what goal you want to achieve.

    
13.08.2017 / 23:26