p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 (http://p2p.wrox.com/forumdisplay.php?f=174)
-   -   associative arrays as objects left out (http://p2p.wrox.com/showthread.php?t=43479)

adspring June 5th, 2006 03:54 AM

associative arrays as objects left out
 
Throughout chapter 3 (object basics), you extensively use "var o = new ClassName();" as a way of creating classes, however only until chapter 6 (DOM basics) do you give us a glimpse of how to create associative arrays (i.e. hashes) with the "checking the Node type" section.

I am a bit confused... Is the associative array way of creating and accessing properties a non-standard approach in OO in JavaScript? It's very Perlish, and a more elaborate explaination of for/against this approach would be most appreciated.

Thanks,
AdSpring.org

nzakas June 5th, 2006 01:37 PM

You raise a good point, perhaps the introduction of associative arrays should come with the discussion of object basics. It's not by any means a non-standard approach since it's a very widely used part of the language. It's all up to your particular requirements.

If you want to create a single instance of an object with no plans of creating more instances, then object literals (associative arrays) are perfectly fine to use. Keep in mind that object literals still inherit from Object, so you'll still get all the properties and methods.

The main drawback is that you lose the ability to create multiple instances and to use prototypes to inherit behavior should you want to extend that object (which may not matter if you really only need one instance).

Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
http://www.nczonline.net/


All times are GMT -4. The time now is 05:55 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.