
www.allitebooks.com www.allitebooks.com Professional JavaScript™ for Web Developers www.allitebooks.com www.allitebooks.com Professional JavaScript™ for Web Developers Nicholas C. Zakas www.allitebooks.com Professional JavaScript™ for Web Developers Copyright © 2005 by Wiley Publishing Inc. All rights reserved. Published by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada 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 Section 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, Inc., 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8700. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or online at 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 PARTIC- ULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMO- TIONAL 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 PRO- FESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOT THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE 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 WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES 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 Depart- ment within the United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. Trademarks: Wiley, the Wiley Publishing logo, Wrox, the Wrox logo, and Programmer to Programmer are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates. JavaScript is a trademark of Sun Microsystems, Inc. in the United States and other countries. 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. Library of Congress Cataloging-in-Publication Data is available from the publisher. ISBN-13: 978-0-7645-7908-0 ISBN-10: 0-7645-7908-8 Printed in the United States of America 10 9 8 7 6 5 4 3 2 1 www.allitebooks.com About the Author Nicholas C. Zakas is a user interface designer for Web applications, specializing in client-side technolo- gies such as JavaScript, HTML, and CSS. Nicholas currently works as Senior Software Engineer, Design Engineering, at MatrixOne, Inc. located in Westford, Massachusetts, USA. Nicholas has a B.S. in Computer Science from Merrimack College, where he learned traditional pro- gramming in C and C++. During college, he began investigating the World Wide Web and HTML in his spare time, eventually teaching himself enough to be hired as Webmaster of a small software company named Radnet, Inc. in Wakefield, Massachusetts, USA. It was there that Nicholas began learning JavaScript and working on Web applications. Nicholas can be reached through his Web site, http://www.nczonline.net/. www.allitebooks.com Credits Vice President and Executive Group Publisher: Development Editor: Richard Swadley Sharon Nash Vice President and Publisher: Senior Production Editor: Joseph B. Wikert Angela Smith Acquisitions Editor: Technical Editor: Jim Minatel Jean-Luc David, Wiley-Dreamtech India Pvt Ltd Editorial Manager: Text Design & Composition: Mary Beth Wakefield Wiley Composition Services www.allitebooks.com Acknowledgments It takes more than just one person to write a book of this nature, despite the single name on the front cover. Without the help of numerous individuals, this book would not have been possible. First are foremost, thanks to everyone at Wiley Publishing, especially Jim Minatel and Sharon Nash, for providing all the guidance and support that a new author needs. Thanks to all those who offered their ideas on what a good JavaScript book should include: Keith Ciociola, Ken Fearnley, John Rajan, and Douglas Swatski. A special thanks to everyone who reviewed the subject matter ahead of time: Erik Arvidsson, Bradley Baumann, Guilherme Blanco, Douglas Crockford, Jean-Luc David, Emil A. Eklund, Brett Fielder, Jeremy McPeak, and Micha Schopman. All your input was excellent and made for a much better book. Thanks to Drs. Ed and Frances Bernard for keeping me in tip-top health during the writing of this book and the past few years. Last, but certainly not least, thanks to my family, mom, dad, and Greg, and my extremely understanding girlfriend, Emily. Your love and support helped take me from the proposal to the final published copy. vii www.allitebooks.com www.allitebooks.com Contents Acknowledgments vii Introduction xxi Chapter 1: What Is JavaScript? 1 A Short History 1 JavaScript Implementations 3 ECMAScript 3 The Document Object Model (DOM) 6 The Browser Object Model (BOM) 9 Summary 9 Chapter 2: ECMAScript Basics 11 Syntax 11 Variables 12 Keywords 15 Reserved Words 15 Primitive and Reference Values 15 Primitive Types 16 The typeof operator 16 The Undefined type 17 The Null type 18 The Boolean type 18 The Number type 18 The String type 20 Conversions 21 Converting to a string 22 Converting to a number 23 Type Casting 24 Reference Types 25 The Object class 26 The Boolean class 27 The Number class 27 ix Contents The String class 29 The instanceof operator 32 Operators 33 Unary operators 33 Bitwise operators 37 Boolean operators 43 Multiplicative operators 46 Additive operators 47 Relational operators 49 Equality operators 50 Conditional operator 52 Assignment operators 52 Comma operator 53 Statements 53 The if statement 53 Iterative statements 54 Labeled statements 56 The break and continue statements 56 The with statement 58 The switch statement 58 Functions 59 No overloading 61 The arguments object 62 The Function class 63 Closures 65 Summary 66 Chapter 3: Object Basics 67 Object-Oriented Terminology 67 Requirements of object-oriented languages 68 Composition of an object 68 Working with Objects 68 Declaration and instantiation 68 Object references 69 Dereferencing objects 69 Early versus late binding 69 Types of Objects 70 Native objects 70 Built-in objects 81 Host objects 87 x Contents Scope 88 Public, protected, and private 88 Static is not static 88 The this keyword 89 Defining Classes and Objects 90 Factory paradigm 90 Constructor paradigm 92 Prototype paradigm 93 Hybrid constructor/prototype paradigm 94 Dynamic prototype method 95 Hybrid factory paradigm 96 Which one to use? 97 A practical example 97 Modifying Objects 99 Creating a new method 99 Redefining an existing method 100 Very late binding 101 Summary 102 Chapter 4: Inheritance 103 Inheritance in Action 103 Implementing Inheritance 104 Methods of inheritance 105 A more practical example 111 Alternative Inheritance Paradigms 115 zInherit 116 xbObjects 120 Summary 124 Chapter 5: JavaScript in the Browser 125 JavaScript in HTML 125 The <script/> tag 125 External file format 126 Inline code versus external files 127 Tag placement 128 To hide or not to hide 129 The <noscript/> tag 130 Changes in XHTML 131 xi Contents JavaScript in SVG 133 Basic SVG 133 The <script/> tag in SVG 134 Tag placement in SVG 135 The Browser Object Model 136 The window object 136 The document object 149 The location object 153 The navigator object 155 The screen object 156 Summary 157 Chapter 6: DOM Basics 159 What Is the DOM? 159 Introduction to XML 159 An API for XML 162 Hierarchy of nodes 163 Language-Specific DOMs 166 DOM Support 167 Using the DOM 167 Accessing relative nodes 167 Checking the node type 169 Dealing with attributes 169 Accessing specific nodes 171 Creating and manipulating nodes 173 DOM HTML Features 178 Attributes as properties 178 Table methods 179 DOM Traversal 182 NodeIterator 182 TreeWalker 187 Detecting DOM Conformance 189 DOM Level 3 191 Summary 191 Chapter 7: Regular Expressions 193 Regular Expression Support 193 Using a RegExp object 194 Extended string methods 195 xii Contents Simple Patterns 197 Metacharacters 197 Using special characters 197 Character classes 199 Quantifiers 201 Complex Patterns 205 Grouping 205 Backreferences 206 Alternation 207 Non-capturing groups 209 Lookaheads 210 Boundaries 210 Multiline mode 212 Understanding the RegExp Object 212 Instance properties 213 Static properties 214 Common Patterns 216 Validating dates 216 Validating credit cards 218 Validating e-mail addresses 222 Summary
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages674 Page
-
File Size-