How to declare a class and how to define its values?

Asked

Viewed 37 times

1

I’m trying to make a Pokemon simulator similar to Pokémon Showdown, but I’m doing it from scratch. In addition to having the functions of a common Pokemón game, you’ll have some customized.

I would like to know how I declare a class and how to define the same values.

In C# after having declared the class model and the new value to it Pessoa numero01 = new Pessoa(); for example, just use as an example numero01.nome = "José";.

According to my notes, it is not the same thing. And now when I try to define the value as follows appears undefined.

class Move { 
   constructor(info, spUsage, atkUsage, baseDamage, name, secEff, accuracy, instaFail, pp)
   {
    this.info = info; 
    this.spUsage = spUsage;
    this.atkUsage = atkUsage;
    this.baseDamage = baseDamage;
    this.name = name;
    this.secEff = secEff;
    this.accuracy = accuracy;
    this.instaFail = instaFail;
    this.pp = pp;
   }
   getMoveData(value)
   {
    //retorna o valor da propriedade inserida. Ex: iceBeam.getMoveData(name); retorna 'Ice Beam'
    return value;
   } 
   setMoveData(data, value)
   {
    //onde 'data' seria um valor da classe como 'info' e 'value' seria o valor para associa-lo ao mesmo como 'true' ou '11' etc.
    data = value;
   }
}

//Começo da database dos movimentos
iceBeam = new Move();
iceBeam.setMoveData(spUsage, true);

1 answer

1


The concept of using class members is wrong. In the class constructor, several properties are defined, such as info and spUsage, this is ok. Now the "set" methods should set to these values, for example:

setSpUsage(value) {
     this.spUsage = value;
}

There are some mistakes:

  • getMoveData is returning the same value it received per parameter, this makes no sense;
  • setMoveData is matching the two values it received per parameter, which also makes no sense.

In this case to assign need or pass values in the constructor, for example var iceBeam = new Move(1,2,3,4....); or directly set the value (iceBeam.info = "teste") or create set methods as the above example by changing the values of the class itself.

Take this example:

class Move { 
   constructor(info, spUsage, atkUsage, baseDamage, name, secEff, accuracy, instaFail, pp)
   {
    this.info = info; 
    this.spUsage = spUsage;
    this.atkUsage = atkUsage;
    this.baseDamage = baseDamage;
    this.name = name;
    this.secEff = secEff;
    this.accuracy = accuracy;
    this.instaFail = instaFail;
    this.pp = pp;
   }

   setSpUsage(value) {
     this.spUsage = value;
   }
}

// definido o valor de "info" no construtor
var iceBeam = new Move("info",null,null,null,null,null,null,null,null);
// definido o valor de "name" diretamente na propriedade
iceBeam.name = "teste";
// definido o valor de "spUsage" usando um método
iceBeam.setSpUsage(true);
// resultado
console.log(iceBeam);

  • 2

    Could make use of setters and getters also, who entered ES6

  • @Ricardo Punctual, my class has several values, would not have a way to make the function set was universal? That is, it "set" all the necessary values of the class?

  • "universal"? use in the constructor can pass all values, or do direct iceBeam.accuracy, iceBeam.... etc

  • @Rafaeltavares yes good suggestion, I declared a set to demonstrate the difference of how it works and as this the other set declared in the class, more to illustrate.

Browser other questions tagged

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