ITworld.com
  Search  
ITworld Home Page ITworld Webcasts ITworld White Papers ITworld Newsletters ITworld News ITworld Topics Careers ITworld Voices ITwhirled Changing the way you view IT
Get with It
JAVASCRIPT --- 08/13/2002

David Wall

Sometimes, you have a lot of operations to perform with the properties and methods of a single object. You might be doing trigonometry and need to make reference to many properties of the Math object, or be implementing a browser-identification script and need to work with the Navigator object a lot. More interestingly, you could be working with custom objects and need to populate their properties at creation time. 

On this topic

The with statement can make your job easier. The following code will work, but it's long and ugly:

sineOfTheTimes = Math.sin(Math.PI/2);
cosineMyMortgage = Math.cos(Math.E); danRatherIsATanGent = Math.tan(Math.PI);

By using with, we make life easier:

with(Math) {

sineOfTheTimes = sin(PI/2);
cosineMyMortgage = cos(E); danRatherIsATanGent = tan(PI); }

Neater!

By the way: You'll recall that, a few weeks ago, we talked about using the document.lastModified property to tag Web pages with the date and time of their last change. The property itself is a Date object, which means you can use various methods to pick out parts of the date and time (the month and the year, for example) in order to reformat the date more attractively. An excellent application that uses the document.lastModified property, complete with a utility that formats the date nicely and gets around turm-of-the-millenium problems, is here:

http://www.chami.com/tips/internet/041198I.html_

 

David Wall works as a freelance writer, programmer, lecturer, and consultant. Based near Washington, D.C., David has written and co- written several books, including Graphics Programming with JFC. David can be reached at David.Wall@itworld.com.



Advertisements
Sponsored links
Locate Hidden Software on business PCs with this free tool
KODAK i1400 Series Scanners stand up to the challenge
Top 5 Reasons to Combine App Performance and Security
Bring harmony to your mix of UNIX-Linux-Windows computing environments
 Home   Newsletters  JAVASCRIPT
www.itworld.com    open.itworld.com     security.itworld.com     smallbusiness.itworld.com
storage.itworld.com     utilitycomputing.itworld.com     wireless.itworld.com

 
Contact Us   About Us   Privacy Policy    Terms of Service   Reprints  

CIO   Computerworld   CSO   GamePro   Games.net   Industry Standard   Infoworld   ITworld  
JavaWorld   LinuxWorld  MacUser   Macworld   Network World   PC World   Playlist  

DEMO   IDG Connect   IDG Knowledge Hub   IDG TechNetwork   IDG World Expo  

Copyright © Computerworld, Inc. All rights reserved

Reproduction in whole or in part in any form or medium without express written permission of Computerworld Inc. is prohibited. Computerworld and Computerworld.com and the respective logos are trademarks of International Data Group Inc.