skip to Main Content

For instace – I need to create a class that can be used to create multiple farms. Each farm must contain an id, a location and an array of animals. Each animal must have a name and quantity (so basically an animal will be an object).
I don’t know how to put this array of animals inside the class.

class Farms {
  constructor (id, location, animals) {
  this.id = id;
  this.location = location;
  this.animals = animals;

} – how can I make ‘animals’ into an array?

4

Answers


  1. If you want to go OOP way then it’s better to create an Animal class as well and pass an array of that.

    class Animal {
      constructor(name, qty) {
        this.name = name
        this.qty = qty
      }
    }
    
    const animal1 = new Animal('A name', 1)
    const animal2 = new Animal('Another name', 2)
    
    const farm = new Farm(1, 'location', [animal1, animal2])
    

    If you don’t want to create Animal class. You can just pass array of objects that defines animal.

    const farm = new Farm(1, 'location', [
      { name: 'A name', qty: 1 },
      { name: 'Another name', qty: 2 }
    ])
    
    Login or Signup to reply.
  2. Create Animal class, instantiate some animals from that class, and pass it to Farm object:

    class Animal {
      constructor(name, quantity) {
        this.name = name;
        this.quantity = quantity;
      }
    }
    class Farm {
      constructor(id, location, animals) {
        this.id = id;
        this.location = location;
        this.animals = animals;
      }
    }
    const animals = [
      new Animal("Bird", 2),
      new Animal("Rabbit", 3),
      new Animal("Dogs", 1)
    ];
    const farm = new Farm(1, "MyFarm", animals);
    console.log(farm);

    Note: It’s generally a good practice to use singular nouns instead of plural nouns for naming classes.
    e.g. name your class Farm instead of Farms.

    Login or Signup to reply.
  3. For the record: without the class sugar and as OOP as anything:

    const farm = Farm( 1, "MyFarm", 
      [Animal(`Pig`, 3), Animal(`Donkey`, 2), Animal()] );
    
    document.body.insertAdjacentHTML(`beforeend`, `
      <pre>${JSON.stringify(farm, null, 2)}</pre>`);
      
    function Animal(name = `unknown`, quantity = 1) {
      return { name, quantity };
    }
    
    function Farm(id, location = `unknown`, animals = []) {
      return { id, location, animals };
    }
    Login or Signup to reply.
  4. Small refactoring to @XMehdi01 answer. Keep constructor arguments followed by an underscore to keep the readability and clarity for future.

    class Animal {
      constructor(_name, _quantity) {
        this.name = _name;
        this.quantity = _quantity;
      }
    }
    class Farm {
      constructor(_id, _location, _animals) {
        this.id = _id;
        this.location = _location;
        this.animals = _animals;
      }
    }
    const animals = [
      new Animal("dog", Math.floor(Math.random() * 100000)),
      new Animal("cat", Math.floor(Math.random() * 100000)),
      new Animal("shark", Math.floor(Math.random() * 100000))
    ];
    let FarmObject = new Farm(crypto.randomUUID(), "Las Vegas", animals);
    console.log(FarmObject);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search