Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html xmlns = "http://www.w3.org/1999/xhtml">
- <head>
- <title>Passing Arrays and Individual Array
- Elements to Functions</title>
- <script type = "text/javascript">
- <!--
- function start()
- {
- var a = [ 1, 2, 3, 4, 5 ];
- document.writeln( "<h2>Effects of passing entire " +
- "array by reference</h2>" );
- outputArray(
- "The values of the original array are: ", a );
- modifyArray( a ); // array a passed by reference
- outputArray(
- "The values of the modified array are: ", a );
- document.writeln( "<h2>Effects of passing array " +
- "element by value</h2>" +
- "a[3] before modifyElement: " + a[ 3 ] );
- modifyElement( a[ 3 ] );
- document.writeln(
- "<br />a[3] after modifyElement: " + a[ 3 ] );
- }
- // outputs "header" followed by the contents of "theArray"
- function outputArray( header, theArray )
- {
- document.writeln(
- header + theArray.join( " " ) + "<br />" );
- }
- // function that modifies the elements of an array
- function modifyArray( theArray )
- {
- for ( var j in theArray )
- theArray[ j ] *= 2;
- }
- // function that attempts to modify the value passed
- function modifyElement( e )
- {
- e *= 2;
- document.writeln( "<br />value in modifyElement: " + e );
- }
- // -->
- </script>
- </head><body onload = "start()"></body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement