This altering var global

Asked

Viewed 43 times

0

I am making a change within the array of the object’s main function, but this is changing several values, including variable outside the object, which cannot 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);

1 answer

0

Warning: the comment space became small, so I had to add a response

How is this change taking place?

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 it leaves everything more confusing), and you didn’t prompt your object correctly.

I don’t 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 );

Also, you need to better describe the problems that occur and what goal you want to achieve.

  • The problem is that in this trackObject.prototype. _setTrackName it is changing the global value of track.name, where this cannot happen.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.