top | item 39846306

(no title)

dorianmariefr | 1 year ago

JavaScript has that with:

    const hello = ({ for: person }) => `Hello ${person}`

    hello({ for: "Dorian" })

    'Hello Dorian'

discuss

order

yen223|1 year ago

Javascript's one is a little bit different in that you are renaming the destructured variable - it's not a function-specific thing - whereas Swift's version is a property of the function.

e.g.

  const user = {id: "123"};
  const { id } = user;          // This extracts the `id` field
  const { id: userId } = user;  // This also extracts the `id` field, but renames it to userId
  console.log(userId)

jahewson|1 year ago

It does indeed, but the cost is an object allocation for every function call.