// 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 11:59, local time: 2024/8/22 22:14,
18.219.40.12: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://piclist.com/tecHREF/language/java/script/definitive/examples/9.2.txt"> language java script definitive examples 9</A> |
Did you find what you needed? |
![]() o List host: MIT, Site host massmind.org, Top posters @none found - Page Editors: James Newton, David Cary, and YOU! * Roman Black of Black Robotics donates from sales of Linistep stepper controller kits. * Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters. * Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated! * Contributors: Richard Seriani, Sr. |
Welcome to piclist.com! |
.