ebook img

JavaScript for Absolute Beginners PDF

494 Pages·2010·8.16 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview JavaScript for Absolute Beginners

JavaScript for Absolute Beginners ■ ■ ■ Terry McNavage JavaScript for Absolute Beginners Copyright © 2010 by Terry McNavage All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN 978-1-4302-7219-9 ISBN 978-1-4302-7218-2 (eBook) Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. President and Publisher: Paul Manning Lead Editors: Ben Renow-Clarke, Matthew Moodie Technical Reviewers: Kristian Besley, Rob Drimmie, Tom Barker Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Mary Tobin Copy Editor: Kim Wimpsett Compositor: MacPS, LLC Indexer: Toma Mulligan Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer- sbm.com, or visit www.springeronline.com. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. ii To the Little Flower, St. Thérèse de Lisieux, for sending me this rose. Contents at a Glance ■Contents ................................................................................................................ v(cid:1) ■About the Author ................................................................................................ xiii(cid:1) ■About the Technical Reviewers .......................................................................... xiv(cid:1) ■Acknowledgments ............................................................................................... xv(cid:1) ■Preface ............................................................................................................... xvi ■Chapter 1: Representing Data with Values ............................................................ 1(cid:1) ■Chapter 2: Type Conversion ................................................................................ 25(cid:1) ■Chapter 3: Operators ........................................................................................... 57(cid:1) ■Chapter 4: Controlling Flow ................................................................................. 97(cid:1) ■Chapter 5: Member Inheritance ........................................................................ 145(cid:1) ■Chapter 6: Functions and Arrays ....................................................................... 181(cid:1) ■Chapter 7: Traversing and Modifying the DOM Tree ......................................... 255(cid:1) ■Chapter 8: Scripting CSS ................................................................................... 307(cid:1) ■Chapter 9: Listening for Events ......................................................................... 347(cid:1) ■Chapter 10: Scripting BOM ................................................................................ 399(cid:1) ■Index ................................................................................................................. 461(cid:1) iv Contents ■Contents at a Glance ............................................................................................ iv(cid:1) ■About the Author ................................................................................................ xiii(cid:1) ■About the Technical Reviewers .......................................................................... xiv(cid:1) ■Acknowledgments ............................................................................................... xv(cid:1) ■Preface ............................................................................................................... xvi ■Chapter 1: Representing Data with Values ............................................................ 1(cid:1) What Are Value Types? ................................................................................................... 1(cid:1) Creating a String Literal .................................................................................................. 2(cid:1) Commenting Code .................................................................................................................................. 2(cid:1) Gluing Strings Together with the + Operator ......................................................................................... 3(cid:1) Creating a Number Literal ............................................................................................... 4(cid:1) Creating a Boolean Literal ............................................................................................... 5(cid:1) Naming a Value with an Identifier ................................................................................... 6(cid:1) Can I Name a Variable Anything I Want? ................................................................................................ 6(cid:1) Some Valid Identifiers Are Already Taken .............................................................................................. 7(cid:1) Creating an Object Literal ............................................................................................... 9(cid:1) Naming Members with Identifiers ........................................................................................................ 12(cid:1) Creating an Array Literal ............................................................................................... 14(cid:1) Creating a Function Literal ............................................................................................ 19(cid:1) Summary ....................................................................................................................... 23(cid:1) (cid:1) v ■ CONTENTS ■Chapter 2: Type Conversion ................................................................................ 25(cid:1) String Members ............................................................................................................ 25(cid:1) Determining the Number of Characters ............................................................................................... 30(cid:1) Decoding or Encoding Characters ........................................................................................................ 31(cid:1) Converting Case ................................................................................................................................... 33(cid:1) Locating a Substring ............................................................................................................................ 35(cid:1) Clipping a Substring ............................................................................................................................. 36(cid:1) Replacing a Substring .......................................................................................................................... 37(cid:1) Splitting a String into an Array of Smaller Strings ............................................................................... 39(cid:1) Searching with Regular Expressions ................................................................................................... 43(cid:1) Explicitly Creating Wrappers ......................................................................................... 43(cid:1) Converting a Value to Another Type .............................................................................. 44(cid:1) Converting a Value to a Number .......................................................................................................... 46(cid:1) Converting a Value to a String ............................................................................................................. 50(cid:1) Putting Off Learning RegExp Syntax .................................................................................................... 53(cid:1) Summary ....................................................................................................................... 56(cid:1) ■Chapter 3: Operators ........................................................................................... 57(cid:1) Introducing Operator Precedence and Associativity ..................................................... 57(cid:1) Using JavaScript Operators .......................................................................................... 60(cid:1) Combining Math and Assignment Operations ...................................................................................... 61(cid:1) Incrementing or Decrementing Values ................................................................................................. 66(cid:1) Testing for Equality .............................................................................................................................. 68(cid:1) Testing for Inequality ........................................................................................................................... 70(cid:1) Comparing Objects, Arrays, and Functions .......................................................................................... 72(cid:1) Determining Whether One Number or String Is Greater Than Another ................................................ 74(cid:1) Determining Whether One Number or String Is Less Than Another ..................................................... 77(cid:1) Greater Than or Equal to, Less Than or Equal to .................................................................................. 78(cid:1) Creating More Complex Comparisons .................................................................................................. 81(cid:1) Saying or With || ................................................................................................................................... 83(cid:1) Saying “and” with && .......................................................................................................................... 84(cid:1) vi ■ CONTENTS Chaining || Expressions ........................................................................................................................ 85(cid:1) Chaining && Expressions ..................................................................................................................... 87(cid:1) Chaining || and && Expressions ........................................................................................................... 89(cid:1) Conditionally Returning One of Two Values ......................................................................................... 90(cid:1) Making Two Expressions Count as One ............................................................................................... 93(cid:1) Deleting a Member, Element, or Variable ............................................................................................ 94(cid:1) Summary ....................................................................................................................... 95(cid:1) ■Chapter 4: Controlling Flow ................................................................................. 97(cid:1) Writing an if Condition .................................................................................................. 98(cid:1) Appending an else Clause .................................................................................................................. 100(cid:1) To Wrap or Not to Wrap ...................................................................................................................... 101(cid:1) Coding Several Paths with the else if Idiom ....................................................................................... 102(cid:1) Controlling Flow with Conditional Expressions .................................................................................. 105(cid:1) Taking One of Several Paths with a Switch ................................................................ 107(cid:1) Writing a while Loop ................................................................................................... 115(cid:1) Aborting an Iteration but Not the Loop ............................................................................................... 118(cid:1) Replacing Break with Return in a Function ........................................................................................ 120(cid:1) Writing a do while loop ............................................................................................... 122(cid:1) Writing a for Loop ....................................................................................................... 125(cid:1) Enumerating Members with a for in Loop ................................................................... 127(cid:1) Snappier Conditionals ................................................................................................. 129(cid:1) Snappier Loops ........................................................................................................... 136(cid:1) Summary ..................................................................................................................... 144(cid:1) ■Chapter 5: Member Inheritance ........................................................................ 145(cid:1) Creating Objects with a Constructor ........................................................................... 145(cid:1) Classical Inheritance ................................................................................................... 149(cid:1) Determining Which Type or Types an Object Is an Instance Of ......................................................... 156(cid:1) Inherited Members Are Shared Not Copied ........................................................................................ 158(cid:1) Modifying New and Past Instances of a Type .................................................................................... 160(cid:1) vii ■ CONTENTS Sharing a Prototype but Forgoing the Chain ...................................................................................... 163(cid:1) Adding an Empty Chain Link .............................................................................................................. 166(cid:1) Stealing a Constructor ....................................................................................................................... 169(cid:1) Prototypal Inheritance ................................................................................................. 171(cid:1) Cloning Members ........................................................................................................ 174(cid:1) Mixins .......................................................................................................................... 176(cid:1) Summary ..................................................................................................................... 179(cid:1) ■Chapter 6: Functions and Arrays ....................................................................... 181(cid:1) Why Use Functions? .................................................................................................... 181(cid:1) Functions Are Values .................................................................................................. 183(cid:1) Function Members ...................................................................................................... 184(cid:1) Conditional Advance Loading ...................................................................................... 185(cid:1) Writing Object.defineProperty() .......................................................................................................... 186(cid:1) Writing Object.defineProperties() ....................................................................................................... 187(cid:1) Writing Object.create() ....................................................................................................................... 188(cid:1) Using the new Functions .................................................................................................................... 189(cid:1) Lazy Loading ............................................................................................................... 194(cid:1) Recursion .................................................................................................................... 198(cid:1) Borrowing Methods with apply() or call() .................................................................... 201(cid:1) Overriding toString() ........................................................................................................................... 201(cid:1) Testing for an Array ........................................................................................................................... 204(cid:1) Rewriting cloneMembers() ................................................................................................................. 206(cid:1) Currying ...................................................................................................................... 208(cid:1) Chaining Methods ....................................................................................................... 212(cid:1) Closure and Returning Functions ................................................................................ 216(cid:1) Passing a Configuration Object ................................................................................... 222(cid:1) Callback Functions ...................................................................................................... 223(cid:1) Memoization ............................................................................................................... 224(cid:1) viii ■ CONTENTS Global Abatement with Modules ................................................................................. 226(cid:1) Arrays .......................................................................................................................... 228(cid:1) Plucking Elements from an Array ....................................................................................................... 229(cid:1) Adding Elements to an Array .............................................................................................................. 233(cid:1) Gluing Two Arrays Together ............................................................................................................... 235(cid:1) Reversing the Elements in an Array ................................................................................................... 237(cid:1) Sorting the Elements in an Array ....................................................................................................... 238(cid:1) Creating a String from an Array ......................................................................................................... 243(cid:1) Taking a Slice of an Array .................................................................................................................. 244(cid:1) Converting a Read-only Array-like Object to an Array ....................................................................... 245(cid:1) Inserting or Deleting Elements from an Array .................................................................................... 249(cid:1) Summary ..................................................................................................................... 253(cid:1) ■Chapter 7: Traversing and Modifying the DOM Tree ......................................... 255(cid:1) DOM Tree .................................................................................................................... 255(cid:1) Is Every Node the Same? ................................................................................................................... 256(cid:1) Interfaces Are Sensibly Named .......................................................................................................... 257(cid:1) Querying the DOM Tree ...................................................................................................................... 257(cid:1) Same Jargon as for a Family Tree ..................................................................................................... 260(cid:1) Traversing the DOM Tree ................................................................................................................... 260(cid:1) Descending with childNodes .............................................................................................................. 260(cid:1) Ascending with parentNode ............................................................................................................... 262(cid:1) Muddying the Waters with Whitespace ............................................................................................. 263(cid:1) Coding Cascade Style ........................................................................................................................ 264(cid:1) Moving Laterally ................................................................................................................................. 268(cid:1) Converting a NodeList to an Array ..................................................................................................... 271(cid:1) Converting a NodeList to an Array for Internet Explorer .................................................................... 273(cid:1) Traversing the DOM without childNodes ............................................................................................ 275(cid:1) Finding an Element by ID ................................................................................................................... 277(cid:1) Finding Elements by Their Tag Names ............................................................................................... 278(cid:1) Finding Elements by Class ................................................................................................................. 279(cid:1) ix ■ CONTENTS Querying Attributes Like a Member ................................................................................................... 281(cid:1) Querying Attributes with Methods ..................................................................................................... 282(cid:1) Querying Attr Nodes ........................................................................................................................... 285(cid:1) Enumerating Attributes for an Element .............................................................................................. 286(cid:1) Creating Element or Text Nodes ........................................................................................................ 288(cid:1) Deleting Content ................................................................................................................................ 292(cid:1) Copying Content ................................................................................................................................. 293(cid:1) Creating Elements with a Helper Function ......................................................................................... 294(cid:1) Reordering Nested Lists ..................................................................................................................... 296(cid:1) Where Did the Formatting Text Nodes Go? ........................................................................................ 302(cid:1) Summary ..................................................................................................................... 304(cid:1) ■Chapter 8: Scripting CSS ................................................................................... 307(cid:1) DOM Interfaces for Working with CSS ........................................................................ 307(cid:1) Clarifying Some CSS Jargon ....................................................................................... 308(cid:1) How Does JavaScript Represent a Rule? ........................................................................................... 308(cid:1) Two Other Declaration Blobs ............................................................................................................. 310(cid:1) Downloading the Sample Files ................................................................................... 310(cid:1) Querying a Style Attribute ........................................................................................... 313(cid:1) Scripting Classes ........................................................................................................ 318(cid:1) Scripting Rules ............................................................................................................ 320(cid:1) Scripting Imported Style Sheets ................................................................................. 326(cid:1) Adding or Deleting a Rule ........................................................................................... 327(cid:1) Adding a Rule to a Style Sheet ........................................................................................................... 328(cid:1) Deleting a Rule from a Style Sheet .................................................................................................... 332(cid:1) Querying Overall Styles from the Cascade .................................................................. 334(cid:1) Enabling and Disabling Style Sheets .......................................................................... 338(cid:1) Including or Importing Style Sheets ............................................................................ 339(cid:1) Embedding a Style Sheet ............................................................................................ 344(cid:1) Summary ..................................................................................................................... 345(cid:1) x

Description:
If you are new to both JavaScript and programming, this hands-on book is for you. Rather than staring blankly at gobbledygook, you'll explore JavaScript by entering and running hundreds of code samples in Firebug, a free JavaScript debugger. Then in the last two chapters, you'll leave the safety of
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.