JS pass by

Functions in JS pass by value and reference.

by Checha Man

HTML

<pre id="aa1"></pre>
<pre id="bb1"></pre>
<p>
 ==================== ====================
</p>
<button onclick="hit()">
  HIT!
</button>
<p>
 ==================== ====================
</p>
<pre id="aa2"></pre>
<pre id="bb2"></pre>
<script>
	function hit() {
    oneone(aa); twotwo(bb);
    document.getElementById('aa1').innerHTML = displaytext(aa);
    document.getElementById('bb1').innerHTML = displaytext(bb);
    window.alert('after!');
  }
</script>

JavaScript 1.7

function oneone(obj) {
  if(obj === null) {
    obj = {
      'left' : 'test',
      'right': 'test'
    };
  }
  else {
    obj = {
      'left': 'test',
      'right': {...obj}
    }
  }
}

function twotwo(obj) {
	if(obj !== null) {
		obj['left'] = 'test';
  }
}


function displaytext(obj) {
	return JSON.stringify(obj, null, 2);
}

let aa = { 'size': 'aa'},
    bb = { 'size': 'bb'};
    
document.getElementById('aa1').innerHTML = displaytext(aa);
document.getElementById('bb1').innerHTML = displaytext(bb);