Passing by Reference Versus Passing by Value
/*
JavaScript Unleashed, Third Edition
by Richard Wagner and R. Allen Wyke
ISBN: 067231763X
Publisher Sams CopyRight 2000
*/
<html>
<head>
<title>JavaScript Unleashed</title>
<script type="text/javascript">
<!--
// wrap and integer inside an object
function intObject() {
this.i;
return this;
}
function start() {
// declare two ways to store an integer
var I;
var myIntObject = new intObject();
// assign initial values
i = 0;
myIntObject.i = 0;
// display current values
document.write("<br>Before<br>");
document.write("i = " + i);
document.write("<br>");
document.write("myIntObject = " + myIntObject.i);
document.write("<br>");
// pass variables
modify(i, myIntObject);
// display current values
document.write("<br>After<br>");
document.write("i = " + i);
document.write("<br>");
document.write("myIntObject = " + myIntObject.i);
document.write("<br>");
}
function modify(n, obj) {
n++;
obj.i++;
}
//-->
</script>
</head>
<body>
<script type="text/javascript">
<!--
start();
//-->
</script>
</body>
</html>
Related examples in the same category