Dan
Dan

Reputation: 121

Passing a value from a class method, as argument, to another method in same class - JS

I'd like my get_hero() class method to take on a random number as an argument.

Is there a way I can get the value from random_hero_id() into it as an argument, and would this be considered good or bad practice?

The random_hero_id() will create and return a random number. The get_hero() will get an URI via the fetch method and return some values.

export default class Player {

  constructor(name, hero_info) {
    this.name = name;
    this.hero_info = hero_info;
    console.log(this.asd)
  }
    // rols the dice and returns a number between 1 - 6, has nothing to do with random_hero_id() or get_hero()
   rollDice() {
    let RandomNumber = Math.floor((Math.random() * 6 + 1 ));
    console.log(RandomNumber)
    return RandomNumber
  }

  random_hero_id() {
    var random_number = Math.floor((Math.random() * 244 + 1));
    console.log(random_number);
    return random_number
  }

  // fetches a random hero and displays some attributes / right now it's only the powerstats
  // I would like this method to take the returned value from the above method random_hero_id()
  get_hero(rand_number) {
    const api_key = '10156555926000957';
    let hero_id = rand_number;
    let hero_url = `https://www.superheroapi.com/api/${api_key}/${hero_id}`;
    console.log(hero_url)
    fetch(hero_url)
    .then(res => { 
      return res.json();
    })
    .then( data => {
      const ps = data.powerstats;

      const power_stats = document.getElementById(this.hero_info);

      power_stats.innerHTML = "";

      const list = document.createElement(`ul`)
      power_stats.appendChild(list)

      Object.entries(ps).forEach(([key, value]) => {
        const smt = document.createElement(`li`)
        smt.innerText = `The heroes ${key} is ${value}`
        list.appendChild(smt)
      })

    })
    .catch(function(err ) {
      console.log('error is ' + err)
    })
  }
}

Upvotes: 0

Views: 81

Answers (2)

Ben Souchet
Ben Souchet

Reputation: 1532

You can add this.random_hero_id in random_hero_id() to store the random hero id, then in get_hero() simply use this.random_hero_id (but be sure to always call random_hero_id() before get_hero(). Or call random_hero_id() directly on the constructor.

Would get the value from random_hero_id() as an argument be considered good or bad practice?

In my opinion this isn't a good practice, the caller don't have to store the id, the player object have to store it internally, so generate the id when you create a player is the best option, then you can have a getter function for this id.

So you can do something like :

constructor(name, hero_info) {
    this.name = name;
    this.hero_info = hero_info;
    this.hero_id = random_hero_id();
}

// ...

get_hero() {
    // You can access hero id with this.hero_id
}

Upvotes: 1

Damon
Damon

Reputation: 4336

Is there a way I can get the value from random_hero_id() into it as an argument

Yes, you would just call get_hero with the return value of random_hero_id like so:

get_hero(random_hero_id())

would this be considered good or bad practice?

This is more of an opinion, but if get_hero is always going to get a random hero, wouldn't it make more sense to get the random number inside get_hero? It appears that get_hero is more general use and is intended to get some entity by id. It might be clearer to create a function called get_random_hero which encapsulates this logic if you are going to do this frequently:

get_random_hero () {
  return get_hero(random_hero_id())
}

that way, get_hero remains general.

Upvotes: 1

Related Questions