Few weeks ago I found on my twitter feed a very interesting blog post: "The Best Frontend JavaScript Interview Questions (written by a Frontend Eng...
For further actions, you may consider blocking this person and/or reporting abuse
cool but proofread your articles :P
I've been writing for about a decade now, but didn't know about that it is possible to bind functions to objects like this:
Is this a relatively new thing or it has always worked like this? Are there any reference material on this?
Maybe, but explain why. The second version has been used ad nauseam by React developers who had to bind the component's prototype methods to the component's instance in order to use them nicely in their JSX templates.
The real ugliness here is that object methods lose their context so easily - but it's also one of the best part of JavaScript, since this makes functions as first class objects.
Which means I can do something like this:
You already got good answers for your final question 🙂
A solution to the last question:
To be very thorough:
Why would you need to freeze it? This stuff is synchronous, how would it change in the middle of your operation?
It's to guard against future changes. But the requirement was to "preserve immutability" and so this makes the original data set immutable going forward.
This approach is the only one that seems to work for me.
const newHeroes = heroes.map(h => {
const newHero = {...h}
newHero.name = newHero.name.toUpperCase()
return newHero
})
Very nice explanation for each scenario. Thanks for the write-up!
thank you its helpful :)
const newHeroes = JSON.parse(JSON.stringify(heroes)).map(h => {
h.name = h.name.toUpperCase()
return h
})
I know this was posted a while ago but here is my answer.
heroes.forEach(hero => Object.freeze(hero));
deep copy this my approach!!!!
adding this line:
const hr=JSON.parse(JSON.stringify(heroes));
It's work fine
Very good but very depressing.
I've been at this for a very long time now and... it's time to give up.
Object.freeze({name: '',family:'', isEvil:''})