When you call by value, you are passing an immutable constant to a function. The function will create a copy locally and that copy will be accessible in a locally scoped variable having the name of the formal parameter.
However, on exit the local value is discarded leaving the original unchanged. It doesn't matter whether you pass a literal constant value or a variable containing a value. This is easier to understand if you already have a good grasp of the scope chain mechanism.
The example demonstrates this.
<HTML>
<HEAD>
</HEAD>
<BODY>
<SCRIPT>
myVar = 22;
callMe(myVar);
document.write("<BR>");
document.write(myVar);
function callMe(aValue)
{
document.write(aValue);
document.write("<BR>");
aValue = 100;
document.write(aValue);
}
</SCRIPT>
</BODY>
</HTML>| See also: | Scope chain |
| Prev | Home | Next |
| Call by reference | Up | Call object |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. | ||