DEV Community

Discussion on: if (!_if) what

Collapse
 
jakebman profile image
jakebman

Is there a way to modify getAnimals() to change the default answer?

Collapse
 
alainvanhout profile image
Alain Van Hout

It could be passed as a(n optional) second parameter.

Collapse
 
mahlongumbs profile image
Mahlon Gumbs • Edited

Edit, I misread the question. Here is the corrected response.

Sure. As @Alain mentioned, you can use an optional parameter.

Optional Parameter

const loadAnimals = () => {  
  animals = {
    dog: "LOVELY",
    cat: "CUTE",
    pony: "AWESOME",
  };
};

const getAnimals = (overrides = {}) => {
  if(!animals) loadAnimals();
  return {
    ...animals,
    ...overrides,
  };
};

getAnimals({
  dog: "GREAT",
  monkey: "AGILE",
}); // -> {dog: "GREAT", cat: "CUTE", pony: "AWESOME", monkey: "AGILE"}

Thanks for your comment.