<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Trim Agency &#187; immutable</title>
	<atom:link href="http://old.trimagency.com/tag/immutable/feed/" rel="self" type="application/rss+xml" />
	<link>http://old.trimagency.com</link>
	<description></description>
	<lastBuildDate>Mon, 29 Jun 2026 06:32:54 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>Immutable Objects using Object.assign</title>
		<link>http://old.trimagency.com/immutable-objects-using-object-assign/</link>
		<comments>http://old.trimagency.com/immutable-objects-using-object-assign/#comments</comments>
		<pubDate>Sat, 21 Jan 2017 20:01:12 +0000</pubDate>
		<dc:creator><![CDATA[Dom Miller]]></dc:creator>
				<category><![CDATA[Today I Learned]]></category>
		<category><![CDATA[immutable]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[object]]></category>

		<guid isPermaLink="false">http://www.trimagency.com/?p=2430</guid>
		<description><![CDATA[<p>Is immutability important? Mutating data can produce code that’s hard to read and error prone. But we can avoid this mess using vanilla javascript&#8217;s ES6 feature Object.assign! Let&#8217;s look at the problem&#8230;. When we changed obj2&#8217;s key property, it also changed obj1&#8217;s key property. No bueno! The solution: As you can see we changed obj2&#8217;s [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://old.trimagency.com/immutable-objects-using-object-assign/">Immutable Objects using Object.assign</a> appeared first on <a rel="nofollow" href="http://old.trimagency.com">Trim Agency</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>Is <em>immutability</em> important? Mutating data can produce code that’s hard to read and error prone. But we can avoid this mess using vanilla javascript&#8217;s ES6 feature Object.assign!</p>
<p>Let&#8217;s look at the problem&#8230;.</p>
<pre class="brush: jscript; title: ; notranslate">
var obj1 = { key: 'some value' };

var obj2 = obj1;
obj2.key = 'another value';

console.log( obj1 === obj2) // true
console.log( obj1.key ) // 'another value'

</pre>
<p>When we changed obj2&#8217;s key property, it also changed obj1&#8217;s key property. No bueno!<br />
The solution:</p>
<pre class="brush: jscript; title: ; notranslate">
var obj1 = { key: 'some value' };

var obj2 = Object.assign( {}, obj1, { key: 'another value' });

console.log( obj1 === obj2) // false
console.log( obj1.key ) // 'some value'
console.log( obj2.key ) // 'another value'

</pre>
<p>As you can see we changed obj2&#8217;s key property and it left obj1&#8217;s state intact! Now that&#8217;s immutable!<br />
Object.assign takes objects as its parameters and passing in an empty object as the &#8216;target&#8217; keeps our &#8216;source&#8217; objects intact. </p>
<p>Object.assign is widely supported by desktop and mobile browsers, please check <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign">Mozilla</a> for more info.</p>
<p>The post <a rel="nofollow" href="http://old.trimagency.com/immutable-objects-using-object-assign/">Immutable Objects using Object.assign</a> appeared first on <a rel="nofollow" href="http://old.trimagency.com">Trim Agency</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://old.trimagency.com/immutable-objects-using-object-assign/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
