Hacker News new | ask | show | jobs
by dorianmariefr 808 days ago
JavaScript has that with:

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

    hello({ for: "Dorian" })

    'Hello Dorian'
2 comments

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)
It does indeed, but the cost is an object allocation for every function call.