JavaScript: The Definitive Guide [Java Script Activate Your Web Pages (Definitive Guides) By David Flanagan 2
JavaScript%20The%20Definitive%20Guide%2C%206th%20Edition
JavaScript%20-%20The%20Definitive%20Guide
06_JavaScript-%20The%20Definitive%20Guide%2C%206th%20Edition
%5BJavaScript%20The%20Definitive%20Guide%20Activate%20Your%20Web%20Pages%20(Definitive%20Guides)%20by%20David%20Flanagan%20-%202
JavaScript%20The%20Definitive%20Guide%2C%206th%20Edition
User Manual: Pdf
Open the PDF directly: View PDF .
Page Count: 1098
Download | ![]() |
Open PDF In Browser | View PDF |
JavaScript: The Definitive Guide SIXTH EDITION JavaScript: The Definitive Guide David Flanagan Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo JavaScript: The Definitive Guide, Sixth Edition by David Flanagan Copyright © 2011 David Flanagan. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com. Editor: Mike Loukides Production Editor: Teresa Elsey Proofreader: Teresa Elsey Indexer: Ellen Troutman Zaig Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Robert Romano Printing History: August 1996: January 1997: June 1998: January 2002: August 2006: March 2011: Beta Edition. Second Edition. Third Edition. Fourth Edition. Fifth Edition. Sixth Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. JavaScript: The Definitive Guide, the image of a Javan rhinoceros, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. ISBN: 978-0-596-80552-4 [LSI] 1302719886 This book is dedicated to all who teach peace and resist violence. Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii 1. Introduction to JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1 Core JavaScript 1.2 Client-Side JavaScript 4 8 Part I. Core JavaScript 2. Lexical Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.1 2.2 2.3 2.4 2.5 Character Set Comments Literals Identifiers and Reserved Words Optional Semicolons 21 23 23 23 25 3. Types, Values, and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 Numbers Text Boolean Values null and undefined The Global Object Wrapper Objects Immutable Primitive Values and Mutable Object References Type Conversions Variable Declaration Variable Scope 31 36 40 41 42 43 44 45 52 53 4. Expressions and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 4.1 Primary Expressions 4.2 Object and Array Initializers 4.3 Function Definition Expressions 57 58 59 vii 4.4 4.5 4.6 4.7 4.8 4.9 4.10 4.11 4.12 4.13 Property Access Expressions Invocation Expressions Object Creation Expressions Operator Overview Arithmetic Expressions Relational Expressions Logical Expressions Assignment Expressions Evaluation Expressions Miscellaneous Operators 60 61 61 62 66 71 75 77 79 82 5. Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 Expression Statements Compound and Empty Statements Declaration Statements Conditionals Loops Jumps Miscellaneous Statements Summary of JavaScript Statements 88 88 89 92 97 102 108 112 6. Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 6.10 Creating Objects Querying and Setting Properties Deleting Properties Testing Properties Enumerating Properties Property Getters and Setters Property Attributes Object Attributes Serializing Objects Object Methods 116 120 124 125 126 128 131 135 138 138 7. Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 7.10 Creating Arrays Reading and Writing Array Elements Sparse Arrays Array Length Adding and Deleting Array Elements Iterating Arrays Multidimensional Arrays Array Methods ECMAScript 5 Array Methods Array Type viii | Table of Contents 141 142 144 144 145 146 148 148 153 157 7.11 Array-Like Objects 7.12 Strings As Arrays 158 160 8. Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 Defining Functions Invoking Functions Function Arguments and Parameters Functions As Values Functions As Namespaces Closures Function Properties, Methods, and Constructor Functional Programming 164 166 171 176 178 180 186 191 9. Classes and Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9 Classes and Prototypes Classes and Constructors Java-Style Classes in JavaScript Augmenting Classes Classes and Types Object-Oriented Techniques in JavaScript Subclasses Classes in ECMAScript 5 Modules 200 201 205 208 209 215 228 238 246 10. Pattern Matching with Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 10.1 Defining Regular Expressions 10.2 String Methods for Pattern Matching 10.3 The RegExp Object 251 259 261 11. JavaScript Subsets and Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 11.1 11.2 11.3 11.4 11.5 11.6 11.7 JavaScript Subsets Constants and Scoped Variables Destructuring Assignment Iteration Shorthand Functions Multiple Catch Clauses E4X: ECMAScript for XML 266 269 271 274 282 283 283 12. Server-Side JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 12.1 Scripting Java with Rhino 12.2 Asynchronous I/O with Node 289 296 Table of Contents | ix Part II. Client-Side JavaScript 13. JavaScript in Web Browsers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 13.1 13.2 13.3 13.4 13.5 13.6 13.7 Client-Side JavaScript Embedding JavaScript in HTML Execution of JavaScript Programs Compatibility and Interoperability Accessibility Security Client-Side Frameworks 307 311 317 325 332 332 338 14. The Window Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341 14.1 14.2 14.3 14.4 14.5 14.6 14.7 14.8 Timers Browser Location and Navigation Browsing History Browser and Screen Information Dialog Boxes Error Handling Document Elements As Window Properties Multiple Windows and Frames 341 343 345 346 348 351 351 353 15. Scripting Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 15.1 15.2 15.3 15.4 15.5 15.6 15.7 15.8 15.9 15.10 Overview of the DOM Selecting Document Elements Document Structure and Traversal Attributes Element Content Creating, Inserting, and Deleting Nodes Example: Generating a Table of Contents Document and Element Geometry and Scrolling HTML Forms Other Document Features 361 364 371 375 378 382 387 389 396 405 16. Scripting CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413 16.1 16.2 16.3 16.4 16.5 16.6 Overview of CSS Important CSS Properties Scripting Inline Styles Querying Computed Styles Scripting CSS Classes Scripting Stylesheets 414 419 431 435 437 440 17. Handling Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 17.1 Types of Events x | Table of Contents 447 17.2 17.3 17.4 17.5 17.6 17.7 17.8 17.9 Registering Event Handlers Event Handler Invocation Document Load Events Mouse Events Mousewheel Events Drag and Drop Events Text Events Keyboard Events 456 460 465 467 471 474 481 484 18. Scripted HTTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491 18.1 Using XMLHttpRequest 18.2 HTTP byThis is a paragraph of HTML
Here is more HTML.