{"componentChunkName":"component---src-templates-blog-post-js","path":"/reference-value/","result":{"data":{"site":{"siteMetadata":{"title":"Noah's Web Dev Notes"}},"markdownRemark":{"id":"c063f940-d1d1-5055-a113-1f6316505656","excerpt":"In javascript, when you assign value to a variable, it has two different ways to achieve that: assign value or assign reference. The main difference between the…","html":"<blockquote>\n<p>In javascript, when you assign value to a variable, it has two different ways to achieve that: assign value or assign reference.</p>\n</blockquote>\n<p>The main difference between the two is that passing by <strong><em>value</em></strong> happens when assigning <strong><em>primitives</em></strong> while passing by <strong><em>reference</em></strong> when assigning <strong><em>objects</em></strong>.</p>\n<h5>value</h5>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">let</span> a<span class=\"token operator\">=</span><span class=\"token number\">10</span>\n<span class=\"token keyword\">let</span> b<span class=\"token operator\">=</span>a\na<span class=\"token operator\">=</span><span class=\"token number\">11</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>a<span class=\"token punctuation\">,</span>b<span class=\"token punctuation\">)</span> <span class=\"token comment\">//11, 10</span></code></pre></div>\n<p>When assign primitives such as string, number, boolean or symbol to a variable. In memory it works like below:</p>\n<blockquote>\n<p>a=10, it generate a key value pair and save them into stack</p>\n</blockquote>\n<table>\n<thead>\n<tr>\n<th align=\"left\">key</th>\n<th align=\"left\">value</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td align=\"left\">a</td>\n<td align=\"left\">10</td>\n</tr>\n</tbody>\n</table>\n<blockquote>\n<p>b=a</p>\n</blockquote>\n<table>\n<thead>\n<tr>\n<th align=\"left\">key</th>\n<th align=\"left\">value</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td align=\"left\">a</td>\n<td align=\"left\">10</td>\n</tr>\n<tr>\n<td align=\"left\">b</td>\n<td align=\"left\">10</td>\n</tr>\n</tbody>\n</table>\n<blockquote>\n<p>a=11</p>\n</blockquote>\n<table>\n<thead>\n<tr>\n<th align=\"left\">key</th>\n<th align=\"left\">value</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td align=\"left\">a</td>\n<td align=\"left\">11</td>\n</tr>\n<tr>\n<td align=\"left\">b</td>\n<td align=\"left\">10</td>\n</tr>\n</tbody>\n</table>\n<h5>reference</h5>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">let</span> a <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span>age<span class=\"token operator\">:</span><span class=\"token number\">10</span><span class=\"token punctuation\">}</span>\n<span class=\"token keyword\">let</span> b <span class=\"token operator\">=</span> a\nb<span class=\"token punctuation\">.</span>age <span class=\"token operator\">=</span> <span class=\"token number\">11</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>a<span class=\"token punctuation\">.</span>age<span class=\"token punctuation\">)</span> <span class=\"token comment\">// 11</span></code></pre></div>\n<p>But if you assign object to a variable, in memory it works like below:</p>\n<blockquote>\n<p>let a = {age:10}, it save the reference address of the object instead of the value of the object in the stack</p>\n</blockquote>\n<table>\n<thead>\n<tr>\n<th align=\"left\">key</th>\n<th align=\"left\">address</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td align=\"left\">a</td>\n<td align=\"left\">address1</td>\n</tr>\n</tbody>\n</table>\n<blockquote>\n<p>and in the heap it save the object value:</p>\n</blockquote>\n<table>\n<thead>\n<tr>\n<th align=\"left\">address</th>\n<th align=\"left\">value</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td align=\"left\">address1</td>\n<td align=\"left\">{age:10}</td>\n</tr>\n</tbody>\n</table>\n<blockquote>\n<p>Therefore, when assign b=a, they will share the same reference address in the stack</p>\n</blockquote>\n<table>\n<thead>\n<tr>\n<th align=\"left\">key</th>\n<th align=\"left\">address</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td align=\"left\">a</td>\n<td align=\"left\">address1</td>\n</tr>\n<tr>\n<td align=\"left\">b</td>\n<td align=\"left\">address1</td>\n</tr>\n</tbody>\n</table>\n<p>and this is why when you change the value of <strong>b</strong>, it will also effect <strong>a</strong></p>\n<blockquote>\n<p>b.age = 11</p>\n</blockquote>\n<table>\n<thead>\n<tr>\n<th align=\"left\">address</th>\n<th align=\"left\">value</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td align=\"left\">address1</td>\n<td align=\"left\">{age:11}</td>\n</tr>\n</tbody>\n</table>\n<h5>Why make reference instead of value?</h5>\n<p>It is because normally object could be huge, and if we all save them as key-value pairs in the stack, it will lead to a heavy memory cost. </p>","frontmatter":{"title":"Reference and value","date":"December 16, 2020","description":"The concept of reference and value."}},"previous":{"fields":{"slug":"/keyword-this/"},"frontmatter":{"title":"Javascript keyword:'this'"}},"next":null},"pageContext":{"id":"c063f940-d1d1-5055-a113-1f6316505656","previousPostId":"7ddbb1fd-8bfe-59c3-a3ae-6514e1d78309","nextPostId":null}},"staticQueryHashes":["2841359383","3257411868"]}