Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Immutable object
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== JavaScript === In [[JavaScript]], all primitive types (Undefined, Null, Boolean, Number, BigInt, String, Symbol) are immutable, but custom objects are generally mutable. <syntaxhighlight lang="javascript"> function doSomething(x) { /* does changing x here change the original? */ }; var str = 'a string'; var obj = { an: 'object' }; doSomething(str); // strings, numbers and bool types are immutable, function gets a copy doSomething(obj); // objects are passed in by reference and are mutable inside function doAnotherThing(str, obj); // `str` has not changed, but `obj` may have. </syntaxhighlight> To simulate immutability in an object, one may define properties as read-only (writable: false). <syntaxhighlight lang="javascript"> var obj = {}; Object.defineProperty(obj, 'foo', { value: 'bar', writable: false }); obj.foo = 'bar2'; // silently ignored </syntaxhighlight> However, the approach above still lets new properties be added. Alternatively, one may use [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze Object.freeze] to make existing objects immutable. <syntaxhighlight lang="javascript"> var obj = { foo: 'bar' }; Object.freeze(obj); obj.foo = 'bars'; // cannot edit property, silently ignored obj.foo2 = 'bar2'; // cannot add property, silently ignored </syntaxhighlight> With the implementation of [https://tc39.es/ecma262/#sec-let-and-const-declarations ECMA262], JavaScript has the ability to create immutable references that cannot be reassigned. However, using a <code>const</code> declaration doesn't mean that value of the read-only reference is immutable, just that the name cannot be assigned to a new value. <syntaxhighlight lang="javascript"> const ALWAYS_IMMUTABLE = true; try { ALWAYS_IMMUTABLE = false; } catch (err) { console.log("Can't reassign an immutable reference."); } const arr = [1, 2, 3]; arr.push(4); console.log(arr); // [1, 2, 3, 4] </syntaxhighlight> The use of immutable state has become a rising trend in JavaScript since the introduction of [[React (JavaScript library)|React]], which favours Flux-like state management patterns such as [[Redux (JavaScript library)|Redux]].<ref>{{cite web|title=Immutability in JavaScript: A Contrarian View|url=http://desalasworks.com/article/immutability-in-javascript-a-contrarian-view/|website=Desalasworks}}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)