w0lf42
05-24-2004, 06:43 PM
I am messing around with passing values to a function via reference and I am under the impression that if I pass an object such as an STRING object or ARRAY object, the function has direct access to the orginal object, which means I can change the values of the values in the object.
The sample code below confuses me. If you run it, you will notice that the ARRAY object will change the value from "November" to "December", but the STRING object does not.
In the head:
<script type="text/javascript">
<!--
function changeString(obj) {
obj = "December";
} // end function
function changeArray(obj) {
obj[0] = "December";
} // end function
// -->
</script>
In the body:
<script type="text/javascript">
<!--
var foo = new String("November");
var bar = new Array("November");
document.writeln(foo + " " + bar[0] + "<br \>");
changeString(foo);
changeArray(bar);
document.writeln(foo + " " + bar[0] + "<br \>");
// -->
</script>
Any suggestions as to why?
Thanks
The sample code below confuses me. If you run it, you will notice that the ARRAY object will change the value from "November" to "December", but the STRING object does not.
In the head:
<script type="text/javascript">
<!--
function changeString(obj) {
obj = "December";
} // end function
function changeArray(obj) {
obj[0] = "December";
} // end function
// -->
</script>
In the body:
<script type="text/javascript">
<!--
var foo = new String("November");
var bar = new Array("November");
document.writeln(foo + " " + bar[0] + "<br \>");
changeString(foo);
changeArray(bar);
document.writeln(foo + " " + bar[0] + "<br \>");
// -->
</script>
Any suggestions as to why?
Thanks