Class javascript

Asked

Viewed 83 times

1

Hello I’m with this error when trying to model a class in javascript:

Player.makePlayer(player,1)
       ^

TypeError: Player.makePlayer is not a function

class player:

class Player {
    constructor() {
      this.id = null;
      this.socketId = null;
      this.name = null;
      this.mmr = null;
    }
    makePlayer(player,socketId){
      this.id = player.id
      this.socketId = socketId
      this.name = player.name
      this.mmr = player.mmr
    }
  }

  module.exports = Player;

I have the same problem in my class match:

class Match {
    constructor() {
      this.id = null
      this.players = null
      this.isActive = null;
    }
    addPlayers(players,socketId){
      this.id = uuid.v4().toString();
      this.socketId = socketId;
      this.players = players;
      this.isActive = false
    }
  }

but in my class match I would need an array of match, and I’m not able to figure out how to solve this.

1 answer

0


What is happening is that you are invoking the instance method makePlayer(player,socketId) as if it were a class method.

I see two possible approaches:

The first approach is to keep the class Player the way it is and create an instance where the instance method call will be made makePlayer().

class Player {
  constructor() {
    this.id = null;
    this.socketId = null;
    this.name = null;
    this.mmr = null;
  }
  makePlayer(player, socketId) {
    this.id = player.id
    this.socketId = socketId
    this.name = player.name
    this.mmr = player.mmr
  }
}

//Instancia a classe Player
let jogador = new Player()

//envoca o método makePlayer na instancia jogador
jogador.makePlayer({
  id: 1,
  name: "Jhon McBrian",
  mmr: null
}, 2345)

//Exibe as informações da instancia jogador
console.log(jogador)

The second approach is to modify the makePlayer() transforming it from an instance method to a class method by adding the keyword static at the beginning of the method declaration, and the makePlayer() one Object Factory class Player.

class Player {
  constructor() {
    this.id = null;
    this.socketId = null;
    this.name = null;
    this.mmr = null;
  }
  
  //Class factory para classe Player
  static makePlayer(player, socketId) {
    let p = new Player();
    p.id = player.id
    p.socketId = socketId
    p.name = player.name
    p.mmr = player.mmr
    return p //O factory deve retornar o objeto construído no método
  }
}

//envoca o factory makePlayer na classe Player
let jogador = Player.makePlayer({
  id: 1,
  name: "Jhon McBrian",
  mmr: null
}, 2345)

//Exibe as informações da instancia jogador
console.log(jogador)

For class match is the same problem and both approaches solve the problem, in case I kept Match.addPlayers() as instance method.

class Match {
  constructor() {
    this.id = null
    this.players = null
    this.isActive = null;
  }
  addPlayers(players, socketId) {
    //Esse sandbox não possui objeto uuid
    this.id = '2c5ea4c0-4067-11e9-8bad-9b1deb4d3b7d' //uuid.v4().toString(); 
    this.socketId = socketId;
    this.players = players;
    this.isActive = false
  }
}

//Inicia um array com duas inst6ancias Match
let arr = [new Match(), new Match()];

//para elemento do array envoca o método addPlayers
arr[0].addPlayers({
  id: 1,
  name: "Jhon McBrian",
  mmr: null
}, 2345)

arr[1].addPlayers({
  id: 2,
  name: "Silio Santos",
  mmr: null
}, 3251)

console.log(arr)

Here using Object Factory in a static method(class method).

class Match {
  constructor() {
    this.id = null
    this.players = null
    this.isActive = null;
  }

  static addPlayers(players, socketId) {
    let m = new Match()
    //Esse sandbox não possui objeto uuid
    m.id = '2c5ea4c0-4067-11e9-8bad-9b1deb4d3b7d' //uuid.v4().toString(); 
    m.socketId = socketId;
    m.players = players;
    m.isActive = false
    return m
  }
}

//Inicia um array 
let arr = [];


arr[0] = Match.addPlayers({
  id: 1,
  name: "Jhon McBrian",
  mmr: null
}, 2345)

arr[1] = Match.addPlayers({
  id: 2,
  name: "Silio Santos",
  mmr: null
}, 3251)

console.log(arr)

  • Hello you could help me on the match?

  • thanks bro!!

  • match? I don’t understand?

  • At the end of the question I posted my class match I’m wondering how to make an array ed my class Match

  • Now that I’ve seen it, you want an array

  • I’ll probably have to have a support class not?

  • Edited.

  • Thanks bro, you think it’s unnecessary to create a support class match manager to do this or it’s unnecessary?

  • It depends on what you want to do. Anything you implement in the code should preferably be easy and useful. If a class manager is useful for development then implement it, but if it has no purpose its implementation is a waste of time.

Show 4 more comments

Browser other questions tagged

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