Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
Node.js/Javascript - Object.setPrototypeOf() not working properly

I have a Player class that has some properties like name, id, score etc... and few methods that modify the score property
Over the course of the execution i end up saving few instances of this class to JSON files. Then later on, I import them back into an array called players. In order to retrieve the class' methods in each object in the array, I did the following:

class Player {
    constructor(_name,_id) {
        this.name = _name;
        this.age = _age;
        this.score = 0;
    }
    addScore(x) {this.score += x}
    multiplyScore(x) {this.score *= x}
    removeScore(x) {this.score -= x}

}
// The function `loadFiles()` returns: [{name:"foo",id:0,score:15},{name:"bar",id:1,score:9}]
const players = loadFiles().map(x => Object.setPrototypeOf(x, Player.prototype));

Normally, when I use the setPrototypeOf() method, it would return an instance of the class I chose with properties values from the targeted object, and then if I do console.log(players[0].constructor.name) it should print out Player except that the objects in players remain exactly the same, the methods aren't added into them and their constructor.name is not Player.
PS: the process works fine if I do this on one object instead of mapping/looping through an array of them.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

There is nothing wrong with the code, it is working completely fine.

class Player {
    constructor(_name,_id) {
        this.name = _name;
        this.age = _age;
        this.score = 0;
    }
    addScore(x) {this.score += x}
    multiplyScore(x) {this.score *= x}
    removeScore(x) {this.score -= x}

}
// The function `loadFiles()` returns: [{name:"foo",id:0,score:15},{name:"bar",id:1,score:9}]
const loadFilesResponse = [{name:"foo",id:0,score:15},{name:"bar",id:1,score:9}];
const players = loadFilesResponse.map(x => Object.setPrototypeOf(x, Player.prototype));
console.log("Score before add: ", players[0].score);
players[0].addScore(3);
console.log("Score after add: ", players[0].score);

It's possible that loadFiles doesn't actually returns the expected Object, for example it could be returning a list of promises (a common mistake when dealing with promises).

about 4 years ago · Juan Pablo Isaza Relatório

0

You have the data, just create objects from that data instead of doing object prototype hackery?

const players = loadFiles().map(p => {
  const { name, age, score } = p;
  p = new Player(name, age);
  p.score = score;
  return p;
});

Although extending that constructor a little would make it less work:

class Player {
  constructor(name, age, score = 0) {
    this.name = name;
    this.age = age;
    this.score = score;
  }
  ...
}

...

const players = loadFiles().map(p => {
  const { name, age, score } = p;
  return = new Player(name, age, score);
});

This you can call the constructor with two or three arguments, and if you call it with two the score argument will simply default to 0. Much easier.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda