// This example is from the book _JavaScript: The Definitive Guide_. // Written by David Flanagan. Copyright (c) 1996 O'Reilly & Associates. // This example is provided WITHOUT WARRANTY either expressed or implied. // You may study, use, modify, and distribute it for any purpose. // Here we create an object representing the date of Christmas, 1996. // The variable xmas contains a reference to the object, not the object itself. xmas =index.html new Date(96, 11, 25); // When we copy by reference, we get a new reference to the original object. solstice = xmas; // Both variables now refer to the same object value. // Here we change the object through our new reference to it solstice.setDate(21); // The change is visible through the original reference, as well. xmas.getDate(); // returns 21, not the original value of 25. // The same is true when objects and arrays are passed to functions. // The following function adds a value to each element of an array. // A reference to the array is passed to the function, not a copy of the array. // Therefore, the function can change the contents of the array through // the reference, and those changes will be visible when the function returns. function add_to_totals(totals, x) { totals[0] = totals[0] + x; totals[1] = totals[1] + x; totals[2] = totals[2] + x; } // Finally, we'll examine comparison by value. // When we compare the two variables defined above, we find they are // equal, because the refer to the same object, even though we were trying // to make them refer to different dates: (xmas == solstice) // evaluates to true // The two variables defined below refer to two distinct objects, both // of which represent exactly the same date. xmas = new Date(96, 11, 25); solstice_plus_4 = new Date(96, 11, 25); // But, by the rules of "compare by reference", distinct objects not equal! (xmas != solstice_plus_4) // evaluates to true
file: /Techref/language/JAVA/script/definitive/examples/9.2.txt, 1KB, , updated: 1997/4/4 10:59, local time: 2024/12/4 18:28,
3.135.205.26:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://sxlist.com/Techref/language/JAVA/script/definitive/examples/9.2.txt"> language JAVA script definitive examples 9</A> |
Did you find what you needed? |
Welcome to sxlist.com!sales, advertizing, & kind contributors just like you! Please don't rip/copy (here's why Copies of the site on CD are available at minimal cost. |
Welcome to sxlist.com! |
.