1. Trang chủ
  2. » Giáo án - Bài giảng

wrox press professional javascript for web developers 2nd (2009)

809 638 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 809
Dung lượng 3,72 MB

Nội dung

Professional JavaScript ® for Web Developers 2nd Edition Nicholas C. Zakas Wiley Publishing, Inc. ffirs.indd vffirs.indd v 12/8/08 12:02:06 PM12/8/08 12:02:06 PM 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 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 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. ffirs.indd viffirs.indd vi 12/8/08 12:02:07 PM12/8/08 12:02:07 PM Contents Foreword xxvii Introduction xxix Chapter 1: What Is JavaScript? 1 A Short History 1 JavaScript Implementations 3 ECMAScript 3 The Document Object Model (DOM) 7 The Browser Object Model (BOM) 9 JavaScript Versions 10 Summary 11 Chapter 2: JavaScript in HTML 13 The <script> Element 13 Tag Placement 15 Deferred Scripts 16 Changes in XHTML 17 Deprecated Syntax 18 Inline Code versus External Files 19 Document Modes 19 The <noscript> Element 21 Summary 22 Chapter 3: Language Basics 23 Syntax 23 Case-sensitivity 23 Identifiers 24 Comments 24 Statements 25 Keywords and Reserved Words 25 Variables 26 Data Types 28 The typeof Operator 28 The Undefined Type 28 The Null Type 30 ftoc.indd xvftoc.indd xv 12/8/08 12:01:07 PM12/8/08 12:01:07 PM Contents xvi The Boolean Type 30 The Number Type 31 The String Type 37 The Object Type 40 Operators 41 Unary Operators 41 Bitwise Operators 45 Boolean Operators 51 Multiplicative Operators 54 Additive Operators 56 Relational Operators 58 Equality Operators 60 Conditional Operator 62 Assignment Operators 62 Comma Operator 63 Statements 63 The if Statement 63 The do-while Statement 64 The while Statement 65 The for Statement 65 The for-in Statement 66 Labeled Statements 67 The break and continue Statements 67 The with Statement 69 The switch Statement 70 Functions 72 Understanding Arguments 74 No Overloading 76 Summary 76 Chapter 4: Variables, Scope, and Memory 79 Primitive and Reference Values 79 Dynamic Properties 80 Copying Values 81 Argument Passing 82 Determining Type 84 Execution Context and Scope 84 Scope Chain Augmentation 87 No Block-Level Scopes 88 ftoc.indd xviftoc.indd xvi 12/8/08 12:01:07 PM12/8/08 12:01:07 PM Contents xvii Garbage Collection 90 Mark-and-Sweep 91 Reference Counting 91 Performance 93 Managing Memory 93 Summary 94 Chapter 5: Reference Types 97 The Object Type 97 The Array Type 100 Conversion Methods 102 Stack Methods 104 Queue Methods 105 Reordering Methods 106 Manipulation Methods 108 The Date Type 109 Inherited Methods 111 Date-Formatting Methods 112 Date/Time Component Methods 113 The RegExp Type 115 RegExp Instance Properties 117 RegExp Instance Methods 118 RegExp Constructor Properties 120 Pattern Limitations 122 The Function Type 122 No Overloading (Revisited) 123 Function Declarations vs. Function Expressions 124 Functions as Values 125 Function Internals 126 Function Properties and Methods 128 Primitive Wrapper Types 130 The Boolean Type 131 The Number Type 132 The String Type 134 Built-in Objects 142 The Global Object 142 The Math Object 146 Summary 149 ftoc.indd xviiftoc.indd xvii 12/8/08 12:01:07 PM12/8/08 12:01:07 PM Contents xviii Chapter 6: Object-Oriented Programming 151 Creating Objects 151 The Factory Pattern 152 The Constructor Pattern 152 The Prototype Pattern 155 Combination Constructor/Prototype Pattern 166 Dynamic Prototype Pattern 166 Parasitic Constructor Pattern 167 Durable Constructor Pattern 169 Inheritance 170 Prototype Chaining 170 Constructor Stealing 175 Combination Inheritance 176 Prototypal Inheritance 177 Parasitic Inheritance 178 Parasitic Combination Inheritance 179 Summary 182 Chapter 7: Anonymous Functions 183 Recursion 184 Closures 185 Closures and Variables 188 The this Object 189 Memory Leaks 190 Mimicking Block Scope 191 Private Variables 193 Static Private Variables 195 The Module Pattern 196 The Module-Augmentation Pattern 198 Summary 199 Chapter 8: The Browser Object Model 201 The window Object 201 The Global Scope 201 Window Relationships and Frames 202 Window Position 205 Window Size 206 Navigating and Opening Windows 207 Intervals and Timeouts 211 System Dialogs 213 ftoc.indd xviiiftoc.indd xviii 12/8/08 12:01:08 PM12/8/08 12:01:08 PM Contents xix The location Object 216 Query String Arguments 216 Manipulating the Location 217 The navigator Object 219 Detecting Plug-ins 221 Registering Handlers 223 The screen Object 224 The history Object 226 Summary 227 Chapter 9: Client Detection 229 Capability Detection 229 Quirks Detection 231 User-Agent Detection 232 History 233 Working with User-Agent Detection 240 The Complete Script 255 Usage 258 Summary 258 Chapter 10: The Document Object Model 261 Hierarchy of Nodes 261 The Node Type 263 The Document Type 269 The Element Type 279 The Text Type 289 The Comment Type 292 The CDATASection Type 293 The DocumentType Type 294 The DocumentFragment Type 294 The Attr Type 296 DOM Extensions 297 Rendering Modes 297 Scrolling 298 The children Property 298 The contains() Method 299 Content Manipulation 300 Working with the DOM 307 Dynamic Scripts 307 Dynamic Styles 309 ftoc.indd xixftoc.indd xix 12/8/08 12:01:08 PM12/8/08 12:01:08 PM Contents xx Manipulating Tables 311 Using NodeLists 314 Summary 314 Chapter 11: DOM Levels 2 and 3 317 DOM Changes 317 XML Namespaces 318 Other Changes 322 Styles 326 Accessing Element Styles 326 Working with Style Sheets 331 Element Dimensions 336 Traversals 342 NodeIterator 344 TreeWalker 347 Ranges 349 Ranges in the DOM 349 Ranges in Internet Explorer 358 Summary 362 Chapter 12: Events 365 Event Flow 365 Event Bubbling 366 Event Capturing 367 DOM Event Flow 367 Event Handlers or Listeners 368 HTML Event Handlers 368 DOM Level 0 Event Handlers 369 DOM Level 2 Event Handlers 370 Internet Explorer Event Handlers 372 Cross-Browser Event Handlers 373 The Event Object 375 The DOM Event Object 375 The Internet Explorer Event Object 379 Cross-Browser Event Object 381 Event Types 383 UI Events 383 Mouse Events 383 Keyboard Events 392 HTML Events 397 ftoc.indd xxftoc.indd xx 12/8/08 12:01:08 PM12/8/08 12:01:08 PM Contents xxi Mutation Events 402 Proprietary Events 407 Mobile Safari Events 417 Memory and Performance 422 Event Delegation 422 Removing Event Handlers 424 Simulating Events 425 DOM Event Simulation 426 Internet Explorer Event Simulation 430 Summary 432 Chapter 13: Scripting Forms 433 Form Basics 433 Submitting Forms 434 Resetting Forms 435 Form Fields 436 Scripting Text Boxes 441 Text Selection 442 Input Filtering 445 Automatic Tab Forward 449 Scripting Select Boxes 450 Options Selection 452 Adding Options 454 Removing Options 455 Moving and Reordering Options 455 Form Serialization 456 Rich Text Editing 458 Interacting with Rich Text 459 Rich Text Selections 462 Rich Text in Forms 463 Summary 464 Chapter 14: Error Handling and Debugging 465 Browser Error Reporting 465 Internet Explorer 465 Firefox 467 Safari 469 Opera 470 Chrome 472 ftoc.indd xxiftoc.indd xxi 12/8/08 12:01:08 PM12/8/08 12:01:08 PM Contents xxii Error Handling 473 The try-catch Statement 474 Throwing Errors 477 The error Event 480 Error-Handling Strategies 481 Identify Where Errors Might Occur 481 Distinguishing between Fatal and Nonfatal Errors 486 Log Errors to the Server 487 Debugging Techniques 488 Logging Messages to a Console 488 Logging Messages to the Page 491 Throwing Errors 491 Common Internet Explorer Errors 492 Operation Aborted 493 Invalid Character 494 Member Not Found 494 Unknown Runtime Error 495 Syntax Error 495 The System Cannot Locate the Resource Specified 496 Debugging Tools 496 Internet Explorer Debugger 496 Firebug 502 Drosera 507 Opera JavaScript Debugger 510 Other Options 513 Summary 513 Chapter 15: XML in JavaScript 515 XML DOM Support in Browsers 515 DOM Level 2 Core 515 The DOMParser Type 516 The XMLSerializer Type 517 DOM Level 3 Load and Save 518 Serializing XML 523 XML in Internet Explorer 523 Cross-Browser XML Processing 528 XPath Support in Browsers 530 DOM Level 3 XPath 530 XPath in Internet Explorer 535 Cross-Browser XPath 536 ftoc.indd xxiiftoc.indd xxii 12/8/08 12:01:08 PM12/8/08 12:01:08 PM [...]... this Forum icon by the forum name in the forum listing For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works as well as many common questions specific to P2P and Wrox books To read the FAQs, click the FAQ link on any P2P page xxxiv flast.indd xxxiv 12/8/08 12:01:47 PM Professional JavaScript for Web Developers 2nd. .. the book p2p .wrox. com For author and peer discussion, join the P2P forums at p2p .wrox. com The forums are a web- based system for you to post messages relating to Wrox books and related technologies, as well as to interact with other readers and technology users The forums offer a subscription feature to e-mail you topics of interest of your choosing when new posts are made to the forums Wrox authors,... hidden frames In short, you learn how to apply JavaScript solutions to business problems faced by web developers everywhere What Does This Book Cover? Professional JavaScript for Web Developers, 2nd Edition, provides a developer-level introduction along with the more advanced and useful features of JavaScript Starting at the beginning, the book explores how JavaScript originated and evolved into what... Messaging Media Elements The Element Offline Support Changes to History Database Storage Drag-and-Drop The WebSocket Type The Future of HTML 5 Summary Chapter 22: The Evolution of JavaScript ECMAScript 4 /JavaScript 2 JavaScript 1.5 JavaScript 1.6 JavaScript 1.7 JavaScript 1.8 JavaScript 1.9 ECMAScript 4 Proposals Variable Typing 647 648 650 655 657 660 660 662 663 666 669 669 670 671 672 672... criteria give implementation developers a great amount of power and flexibility for developing new languages based on ECMAScript, which partly accounts for its popularity ECMAScript Support in Web Browsers Netscape Navigator 3 shipped with JavaScript 1.1 in 1996 That same JavaScript 1.1 specification was then submitted to Ecma as a proposal for the new standard, ECMA-262 With JavaScript s explosive popularity,... interfaces for working with the content of a web page ❑ The Browser Object Model (BOM), which provides methods and interfaces for interacting with the browser There are varying levels of support for the three parts of JavaScript across the five major web browsers (Internet Explorer, Firefox, Chrome, Safari, and Opera) Support for ECMAScript edition 3 is generally good across all browsers, whereas support for. .. regular expressions in JavaScript The book then takes all this knowledge and applies it to creating dynamic user interfaces The last part of the book is focused on advanced topics, including performance and memory optimization, best practices, and a look at where JavaScript is going in the future Who Is This Book For? This book is aimed at the following three groups of readers: ❑ ❑ Web application developers. .. ISBN This book’s ISBN is 978-0-470-22780-0 After you download the code, just decompress it with your favorite compression tool Alternately, you can go to the main Wrox code download page at www .wrox. com/dynamic/books/download.aspx to see the code available for this book and all other Wrox books Errata We make every effort to ensure that there are no errors in the text or in the code However, no one... first edition of Professional JavaScript for Web Developers in 2005 At the time, my colleagues and I were working at Yahoo! to create the Yahoo! User Interface Library (YUI) as a foundation for front-end engineering here and to evangelize best practices in our nascent discipline Every Friday, we’d gather in a classroom to talk about the front-end engineering and to teach classes on JavaScript, CSS,... talked Zakas into coming to Yahoo! to help shape the front-end engineering community here What Zakas accomplished with Professional JavaScript for Web Developers is singular: He treated JavaScript as a subject that is both serious and accessible If you are a programmer, you will learn where JavaScript fits into the broader spectrum of languages and paradigms with which you’re familiar You’ll learn how its . Professional JavaScript ® for Web Developers 2nd Edition Nicholas C. Zakas Wiley Publishing, Inc. ffirs.indd vffirs.indd v 12/8/08 12:02:06 PM12/8/08 12:02:06 PM Professional JavaScript ® for. 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. learn how to apply JavaScript solutions to business problems faced by web developers everywhere. What Does This Book Cover? Professional JavaScript for Web Developers , 2nd Edition, provides

Ngày đăng: 28/04/2014, 17:15