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 🙂
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.
A solution to the last question:
Very nice explanation for each scenario. Thanks for the write-up!
const newHeroes = heroes.map(h => {
const newHero = {...h}
newHero.name = newHero.name.toUpperCase()
return newHero
})
deep copy this my approach!!!!
adding this line:
const hr=JSON.parse(JSON.stringify(heroes));
It's work fine
const newHeroes = JSON.parse(JSON.stringify(heroes)).map(h => {
h.name = h.name.toUpperCase()
return h
})
thank you its helpful :)
I know this was posted a while ago but here is my answer.
heroes.forEach(hero => Object.freeze(hero));
Object.freeze({name: '',family:'', isEvil:''})
Very good but very depressing.
I've been at this for a very long time now and... it's time to give up.