Pass by Value and Reference

by Nico D

JavaScript

document.write("<h3>Function Pass by Value and Value containing Reference</h3>"); 

function f(aVal, bRef, cRef) {
	document.write("Before Function => aVal:" + aVal + " ~ bRef:[" + bRef + "] ~ cRef:" + cRef.first + "<br/>");
  aVal = 1;
  bRef.push("link");
  cRef.first = false;
	document.write("After Function => aVal:" + aVal + " ~ bRef:[" + bRef + "] ~ cRef:" + cRef.first + "<br/><br/>");
}
var x = 4;
var y = ["eeny", "miny", "mo"];
var z = {
  first: true
};
f(x, y, z);
document.write("No Change by Function => x:" + x + "<br/>Change by Function => y:[" + y + "] ~ z:" + z.first + "<br/>");

document.write("<h3>Using References</h3>");

var a = ["1", {
  link: "2"
}];
var b = a[0];
var c = a[1];
document.write("a: ["+a+"]<br/>b:" + b + " ~ c.link:" + c.link + "<br/><br/>");
a[0] = "3";
a[1] = "4";
document.write("a: ["+a+"]<br/>b:" + b + " ~ c.link:" + c.link + "<br/>");