8 ms·
JavaScript has that with: const hello = ({ for: person }) => `Hello ${person}` hello({ for: "Dorian" }) 'Hello Dorian'
by dorianmariefr 2y ago
JavaScript has that with:
const hello = ({ for: person }) => `Hello ${person}`
hello({ for: "Dorian" })
'Hello Dorian'
- yen223 2y agoJavascript'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 2y agoIt does indeed, but the cost is an object allocation for every function call.