-1
I was watching a lecture on this subject and went to reproduce the code, but with Arrow Function.
It was like this:
const heroes = [
{ name: 'Batman', realName: 'Bruce Wayne' },
{ name: 'Joker', realName: 'Arthur Fleck'}
];
const names = heroes.map(
function({ name }) {
return name;
}
);
console.log (names) // ["Batman","Joker"]
And I decided to try to put with Arrow Function, thinking that it would have the same result, in that part I put this way:
const heroes = [
{ name: 'Batman', realName: 'Bruce Wayne' },
{ name: 'Joker', realName: 'Arthur Fleck'}
];
const names = heroes.map( (name) => {
return name;
});
console.log(names)
But the result came out different, printou name
and realName
, the whole object of the two.
So, Rayane, your question is the same one I had a long time ago, so it’s probably gonna be marked as a repeat question. You can find the post at https://answall.com/questions/472205/diff%C3%a7as-entre-definam%C3%a9todos-de-objects-using-Arrow-Function-e-function
– t_carvalho
@t_carvalho and @Augusto, I do not believe it is duplicated because the functions are not being used as methods and do not even make use of the
this
. Actually the problem was using destructuring assignment infunction
and not use in Arrow Function (as answered below)– hkotsubo
I wish I had voted to reopen, but my vote is hammered into Javascript, so it ended up reopening the question at once. I agree with @hkotsubo that the question is not duplicated.
– Luiz Felipe
Really @hkotsubo, I must have been mistaken when I understood the doubt being related to the question of scope, not destructuring itself. Thanks for the correction!
– t_carvalho