DEV Community

Discussion on: JavaScript clone and rewrite property from existing object

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Eh? The easiest way to overwrite a property is just to set it on the object:

user.online = true
Enter fullscreen mode Exit fullscreen mode

Your examples are not overwriting the original object at all - they are creating a totally new object and overwriting the property on that. The original user object will remain unchanged

Collapse
 
dailydevtips1 profile image
Chris Bongers

You are completely right Jon,

I forget to mention a context where you want to keep the original existing.
I use this method for updating states that don't need to refresh in the UI (React)

Could have probably worded it a bit differently

Collapse
 
peerreynders profile image
peerreynders

I wonder if in this context "Rewriting JavaScript object properties" might be a better way of putting it.

Strangely enough "updating" an Elixir map

%{expenses | groceries: 150, commute: 75}
Enter fullscreen mode Exit fullscreen mode

or an Elm record

{ steve | name = "Wozniak" } 
Enter fullscreen mode Exit fullscreen mode

creates an entirely new value as well (because everything is immutable).