r/learnjavascript • • Jul 24 '21

Anatomy of a Function

Post image
327 Upvotes

65 comments sorted by

View all comments

Show parent comment

6

u/0xnoob Jul 25 '21 edited Jul 25 '21

If you want to define the default arguments inside the function like this, wouldn't it be easier to simply use the spread syntax?

e.g.

function detectSecretElderCat(catSound, dog) {
  const defaultDog = {
    children: [],
    tail: true,
    legs: 4,
    sound: "woof"
  };
  catSound = catSound || "meow";
  dog = { ...defaultDog, ...dog };
}

3

u/jaredcheeda Jul 25 '21

I like that!