0

I have an Animal Object that looks like this:

AnimalObject {attributes: {…}, locations: {…}}

Animal Object Class:

export default class AnimalObject {
  constructor(apiResult, included) {
    this.attributes = apiResult.attributes;
    this.locations = apiResult.locations;

    ...
  }

I now have a specific AnimalObject called FishObject that has some fish related methods.

export default class FishObject extends AnimalObject {

  getFood(){
   const foods = this.getFishFood(this.attributes)
   return foods
  }

  talk(){
    return "blub"
  }
}

Is there a way in javascript that I can cast a list of Animal Objects [{AnimalObject}, {AnimalObject}] into Fish objects? I want to map through the AnimalObject list and call a fish method.

--Update: I'm most likely going to instantiate with the Fish class...

lost9123193
  • 10,460
  • 26
  • 73
  • 113
  • Looks like a code smell and that those objects should have been `FishObject` to begin with. – Joseph Oct 14 '20 at 22:20
  • (A class _is an object_) --- Also may you explain what you mean by cast? – evolutionxbox Oct 14 '20 at 22:24
  • @evolutionxbox it allows you to change the object type. It's used in Java – lost9123193 Oct 14 '20 at 22:25
  • @Joseph hmm.. ok point taken – lost9123193 Oct 14 '20 at 22:25
  • @lost9123193 I'm confused. JavaScript isn't Java. Have you seen something to say that casting is possible in JavaScript? – evolutionxbox Oct 14 '20 at 22:26
  • @evolutionxbox, a `class` is not an Object, it's actually a constructor function sugar syntax. An Object is created from a constructor or class after `new` is called on it. OP probably just wants to do like: `const fishObj = new AnimalObject(argsHere), dogObj = new AnimalObject; /* without args you don't need parameter when new */`. The point of a generic `class AnimalObject` should be reuse for multiple animal types. – StackSlave Oct 14 '20 at 22:33
  • @evolutionxbox `class Test{ constructor(){ this.test = 'worked'; } }; console.log(typeof Test);`. Hope you learned something. – StackSlave Oct 14 '20 at 22:38
  • `function`s have a `prototype` property that is an Object, but the `function` is still just a `function`... calling `new` creates an Object out of the `function` as long as it has `this` references. @evolutionxbox, it will help the OP and yourself to know how it works. I even have books that teach incorrectly, so don't feel bad. – StackSlave Oct 14 '20 at 22:43
  • @evolutionxbox, that's great. Let's not call construtors or classes Objects then. It will confuse OP. – StackSlave Oct 14 '20 at 22:50
  • 1
    @StackSlave No. I'm have deleted my comments. btw - https://stackoverflow.com/questions/5958417/javascript-function-and-object – evolutionxbox Oct 14 '20 at 22:51

1 Answers1

0

you can handle this with the Object.assign() method. If you wrap it like below :

function castToFish(animalObject) {
     return Object.assign(new FishObject(),  animalObject)
}

Then you can transform your array with map :

var fishArray = [...someAnimalObjects].map(castToFish)

Of course, I assumed that you can use es6 syntax.

Joffrey K
  • 185
  • 9