constheroes=[{name:'Wolverine',family:'Marvel',isEvil:false},{name:'Deadpool',family:'Marvel',isEvil:false},{name:'Magneto',family:'Marvel',isEvil:true},{name:'Charles Xavier',family:'Marvel',isEvil:false},{name:'Batman',family:'DC Comics',isEvil:false},{name:'Harley Quinn',family:'DC Comics',isEvil:true},{name:'Legolas',family:'Tolkien',isEvil:false},{name:'Gandalf',family:'Tolkien',isEvil:false},{name:'Saruman',family:'Tolkien',isEvil:true}];functioncopyObject(obj,cb){letcallbackPassed=false,stringToObj=null,objToString=null;if(cb&&typeofcb!=="function")thrownewError("Second Argument must be function.");elsecallbackPassed=true;objToString=JSON.stringify(obj);stringToObj=JSON.parse(objToString);if(callbackPassed)cb(stringToObj);returnstringToObj;}constnewHeroes=heroes.map(h=>{returncopyObject(h,function(obj){obj.name=obj.name.toUpperCase();});});// you can copy Objects with JSON :)// Object.assign is better solution for this kind of problems.
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.
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.
Hmmm... can't really think of a summary. Trying to get from a work history that's all over the place to firmly on a career path. Very tough being a Jr. developer in SF cause everyone else seems to ...
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:
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!
Very good but very depressing.
I've been at this for a very long time now and... it's time to give up.
thank you its helpful :)
deep copy this my approach!!!!
adding this line:
const hr=JSON.parse(JSON.stringify(heroes));
It's work fine
I know this was posted a while ago but here is my answer.
heroes.forEach(hero => Object.freeze(hero));
const newHeroes = JSON.parse(JSON.stringify(heroes)).map(h => {
h.name = h.name.toUpperCase()
return h
})
Object.freeze({name: '',family:'', isEvil:''})