Professional JavaScript for Web Developers, 2nd Edition
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright 2009 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-0-470-22780-0
Library of Congress Cataloging-in-Publication Data
Zakas, Nicholas C.
Professional JavaScript for web developers/Nicholas C. Zakas. 2nd ed.
p. cm.
Includes index.
ISBN 978-0-470-22780-0 (paper/website)
1. Web site development. 2. JavaScript (Computer program language) I. Title.
TK5105.8885.J38Z34 2008
005.2'762 dc22
2008045552
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions .
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read.
For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. JavaScript is a registered trademark of Sun Microsystems, Inc. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books.
Dedicated to my family: mom, dad, Greg, Yiayia, and Papou. We may be few in numbers, but we are mighty! Your constant love and support have made the past couple of years possible.
About the Author
Nicholas C. Zakas has a B.S. in Computer Science from Merrimack College and an M.B.A. from Endicott College. He is the coauthor of Professional Ajax, Second Edition (Wiley, 2007) as well as dozens of online articles. Nicholas works for Yahoo! as a principal front-end engineer on Yahoo!s front page and a contributor to the Yahoo! User Interface (YUI) Library. He has worked in web development for more than eight years, during which time he has helped develop web solutions in use at some of the largest companies in the world.
Nicholas can be reached through his web site www.nczonline.net .
Credits
Acquisitions Director
Jim Minatel
Senior Development Editor
Kevin Kent
Technical Editor
Alexei Gorkov
Development Editor
Gus Miklos
Production Editor
Rebecca Coleman
Copy Editors
Foxxe Editorial Services, Candace English
Editorial Manager
Mary Beth Wakefield
Production Manager
Tim Tate
Vice President and Executive Group Publisher
Richard Swadley
Vice President and Executive Publisher
Joseph B. Wikert
Project Coordinator, Cover
Lynsey Stanford
Proofreader
Kathryn Duggan
Indexer
Jack Lewis
Acknowledgments
It takes many people to create a single book, and Id like to thank some people here for their contributions to this work.
First and foremost, thanks to everyone at Wiley for their support: Jim Minatel for once again putting his faith in me; Kevin Kent for dealing with the hectic outline rearrangements I tend to make throughout writing; and Alexei Gorkov, the best technical editor in the world, who makes sure that everything I say is 100-percent accurate.
A big thanks to everyone who provided feedback on draft chapters: David Serduke, Julian Turner, Pete Frueh, Chris Klaiber, Stoyan Stefanov, Ross Harmes, and David Golightly. Your early feedback was really helpful in making this book what it is today.
Last, thanks to Eric Miraglia for his contribution of a foreword. Eric is the reason I ended up at Yahoo!, and it has been a pleasure to work with him for the past two years.
Contents
The Element
Document Modes
The Element
Summary
Chapter 3: Language Basics
Syntax
Keywords and Reserved Words
Variables
Data Types
Operators
Statements
Functions
Summary
Chapter 4: Variables, Scope, and Memory
Primitive and Reference Values
Execution Context and Scope
Garbage Collection
Summary
Chapter 5: Reference Types
The Object Type
The Array Type
The Date Type
The RegExp Type
The Function Type
Primitive Wrapper Types
Built-in Objects
Summary
Chapter 6: Object-Oriented Programming
Creating Objects
Inheritance
Summary
Chapter 7: Anonymous Functions
Recursion
Closures
Mimicking Block Scope
Private Variables
Summary
Chapter 8: The Browser Object Model