首页 计算机 考试教辅
JavaScript The Definitive Guide_David Flanagan_英文版 pdf电子书免费下载
首页 > 计算机 > javascript > JavaScript The Definitive Guide_David Flanagan_英文版 pdf电子书免费下载

《JavaScript The Definitive Guide_David Flanagan_英文版》pdf电子书免费下载






JavaScript The Definitive Guide_David Flanagan_英文版




《JavaScript The Definitive Guide_David Flanagan_英文版》介绍

The sixth edition covers HTM I 5and ECMAScript 5.Many chaptershave been completely rewritten—Brendan Eichto bring them inline with to dayscreator of JawaS cptC TO of Mozi abest web development practices.New chapters in this editiondocument j Query and server-side JavaScriptTa palo usRecommended for experiencedprogrammers who want to learnthe programming language of the—Tom RobinsonWeb, and for current Java Scripscofounder of 280Nort, co crestor of Cappuccinoprogrammers who want tomaster ilDavid Flanagan is also the author一L.Chris Andersonof The Ruby Programmingcofounder of Couch Base, Apache Couch DB commit erLanguage J are e in a Nuts bel, andon do uthor of Couch DB:The Definitive GuideJara Script Pocket Reference:Previous programming experie rcerecommended.USS 49.99CANS 57.99ISBN:978-0-596-80552-49780596805524The Definitive GuideThe Essential JavaScript ReferenceSince 1996.Jaca Script:7heDefinitite Guide has been thebible for Java Scrips programmers.With more than 500.000copies in print, web developers are still raving about it:“A must-have reference for expert JavaScriptprogrammer....well-organized and detalle d. “I made a career of what I learned from JavaScript:The Definitive Guide. “The Definitive Guide taught me JavaScript “l know which parts of JavaScript matter, based on howcrinkled the spine of my copy of The Definitive Guide isin that section, “...an indispensable reference for all JavaScriptdevelopers.If there's something I need to know aboutJavaScript, I trust The Definitive Guide wil have theright answer for me.It's that good. Me soft Cen-Web Commun y Pogram Manager and jOweymmeberTwitter:@o reily mediafacebook.com/oreilyO'REILLY°oreilly.com54992

SIXTH EDITIONO'REILLY'JavaScript:The Definitive GuideDavid FlanaganBeijing·Cam bid ge·Fam ham·Koln·Sebastopol·TokyoJava Scr pt:The Definit ve Guide, Sixth Editionby David FlanaganCopyrghtO2011Davd Flanagan.All rghts reserved:Prn red in the United States of America.Pub ished by OReilly Media, In e, , 1005 Gravenstein Highway North, Sebastopol, CA 95472O'Relly books maybe purchased for educational, business, or sales promocion al use.Online editionsarea eavabictormesttestatp.ysa anta one two.For more mtn mtn, cont tearcorporate/instutionalsalesdepartment:(BO0)998-993Borcorpuratetoreilly.com.Editor:Mike Lou kid esIndexer:Ellen Troutman Z aigProduction Editor:Teresa ElseyCover Designer:Karen Moni gomeryProofreader:Teresa ElseyInte or Deine:Davd Fu aoPrinting History:August 1996:Beta Edition,nuy 197Second Edition,June 1998:Third EditionJan tary 2002Fourth Edition.Aug usr 2006Fifth Edit on.March 2011Sixth Edition.Nur shell Handbook, the Nur shell Handbook logo, and the O Reily logo are registered trademarks ofO'Relly Meda, In e./a vaSc npt:The Defini live G wie, the image of a lavan rhino cet os, and related tradedres are trade mat ks of O'Rly Media, IncMany of the designations used by manufacturers and sellers to distinguish their pra ducts are claimed astrade marles.Where t has e designations appear in this book, and OReilly Media, Inc., was aware of atrademark dl aim, the designations have been pnn ted in caps or in i rial caps.While every precaution has been taken in the preparation of this book, the publisher and authors assumenote sponsibility for errors or omissions, or for damages resulting from the use of the information con-tained herein:ISBN:97H-0-596-80552-4


This book covers the JavaScript language and the JavaScript APLs implemented by webbrowsers.I wrote it for readers with atleast some prior programming experience whowant to learn JavaScript and also for programmers who already use JavaScript but wantto take their understanding to a new level and really master the language and the webplatform.My goal with this book is to document the JavaScript language and platformcomprehensively and definitively.As a result, this is a large and detailed book.My hope,however, is that it will reward careful study, and that the time you spend reading it willbe easily recouped in the form of higher programming productivity.This book is divided into four parts.Part I covers the JavaScript language itself.PartIIcoversclient-sideJavaScript:theJavaScriptAPIsdefinedbyHTML5andrelatedstandards and implemented by web browsers.Part Ill is the reference section for thecore language, and Part lV is the reference for client-side JavaScript.Chapter l includesan our line of the chapters in Parts I and II(see 81.1)This sixth edition of the book covers both ECMAScript 5(the latest version of the corelanguage) and HTML 5(the latest version of the web platform) .You'll findECMAScript 5 material throughout Part I.The new material onHTML5is mostly inthe chapters at the end of Part II, but there is also some in other chapters as well.Completely new chapters in this edition include Chapter 11, JavaScript Subsets andExtensions; Chapter 12, Server-Side JavaScript; Chapter 19, The j Query Lil rary; andChapter 22, HTML SAP Is,Readers of previous editions may notice that l have completely rewritten many of thechapters in this book for the sixth edition.The corc of Part I—the chapters coveringobjects, arrays, functions, and classes—is all new and brings the book inline withcurrent programming styles and bestpractices.Similarly, key chapters of Part II, suchas those covering documents and events, have been completely re wnt ten to bring themup-to-date.If you are reading a digital version of this book that you for your employer) did not payfor for borrow from someone who did) then you probably have an illegally pirated copyxiiA Note About PiracyWriting the sixth edition of this hook was a full-time job, and it took more than a year.The only way I get paid for that time is when readers actually buy the book.And theonly way l can afford to work on a seventh edition is if I get paid for the sixth.1do not condone piracy, but if you have a pirated copy i goahead and read a couple ofchapters.I think thar you'll find that this is a valuable source of information abourJavaScript, better organized and of higher quality than what you can ind freely(andlegally) available on the Web.If you agree that this is a valuable source of info m mation,then please pay for thar value by purchasing a legal copy(either digital or print) of thebook.On the other hand, if you find thar this book is no more valuable than the freeinformaion on the web, then please discard your pirated copy and use those freeinto rmation sources.Conventions Used in This BookI use the following typographical conventions in this book:ItalicIs used for emphasis and to indicate the frst use of a term.Italic is also used foremail addresses, URIs and filenames.Constant widthIs used in all JavaScript code and CSS and HTML listings, and generally for any-thing that you would type literally when programming.Const on twidth i to lichtp:/oreilly.com/catalog/9780596805531/ls used for the names of function parameters, and generally as a placeholder toindicate an item that should be replaced with an actual value in your program.Example CodeThe examples in this book are available online.You can find them inked from thebook's catalog page at the publisher's website:This hook is here to help you get your job done.In general, you may use the code inthis book in your programs and documentation.You do not need to contactO'Reillyfor permission unless you're reproducing a significant portion of the code.For example,writing a program that uses several chunks of code from this book does not requirepermission.Sell ng or distributing a CD-ROM of examples from O'Reilly books doesrequire permission.Answering a question by citing this book and quoting examplexv|Pre lace

code does not require permission.Incorporating a significant amount of example codefrom this book into your product's documentation does require permissionIf you use the code from this book.l appreciate, but do not require, attribution.Anattn bution usually includes the title, author, publisher, and ISBN.For example:“Java-Script The Definite Gude, by David Flanagan(O'Rey) .Copynght2011DavdFlnag an, 978-0-596-80552-4.”For more details on the O'Reilly code reuse policy,sechitp://oreily.com/publalorciy/ask_ti/2001/code policy.html.If you feel your use of the examples falls outside of thepermission given above,feelfreetocontactOReillyatpermtissions@oreilly.comErrata and Howto Contact UsThe publisher maintains a public list of errors found in this book.You can view thelist, and submit the errors you find, by visiting the hook's webpageTo comment or ask technical questions abour this book, send c mailto:For more information about our books, conferences, Resource Centers, and theO'Reilly Network, sec our website at:Many people have helped me with the creation of this book.Id like to thank my editor,Mike Lou kid es, for i rying to keep me on schedule and for his insightful commentsThanks also to my technical reviewers:Zachary Kess in, who reviewed many of thechapters in Part l, and Raffaele Cecco, who reviewed Chapter 19 and thematerial in Chapter 21.The production team a tO'Reilly has done their usual fine jobDan Faux smith managed the production process, Teresa Elsey was the productioneditor, Rob Romano drew the figures, and Ellen Trou mm anZai g created the indexIn this era of effortless electronic communication, it is impossible to keep track of allthose who influence and inform us.Td like to thank everyone who has answered myquestions on the es 5.w3c, and whatwg mailinglists, and everyone who has shared theirinsightful ideas abour Java Scrip r programming online.I'm sorry I can't list you all byname, but it is a pleasure to work within such a vibrant community of Java Seri ptEditors, reviewers, and cont n but ors to previous editions of this book have includedAndrew Schulman, Angelo Sir igos, Aristotle Pagaltzis, Brendan Eich, ChristianHeilmann, Dan Shafer, DaveC Mitchell, Deb Cameron, Douglas Crockford, Dr.Tank red Hirschmann, Dylan Schiemann, Frank Wil is on, Geoff Stearns, Herman Ven·ter, Jay Hodges, Jeff Yates, Joseph Kesselman, Ken Cooper, Larry Sulla n, Lynn Rollins, Neil Berkman, Nick Thompson, Norn is Boyd, Paula Ferguson, Peter-Paul Koch,Philippe Le Hegaret, Richard Y aker, Sanders Kleinfeld, Scott Furman, Scott Issacs,Shon Katzenberg er, Terry Allen, Todd Ditch end orf, Vid urAp para o, and WaldemarThis edition of the book is substantially re writ i en and kept me away from my familyfor many late nights.My love to them and my thanks for putting up with my absenceshttp://oreilly.com/catalog/9780596805531baokquestions@oreilly.comhttp://www.oreiliy.comFindusonFacebook:hutp:/ffacebook.com/oreilyFollowusonTwitter:http:/twilter.com/oreillymediaWatchusonYouTube:http://www.youtube.com/oreillymedia

CHAPTER 1Introduction to JavaScript

JavaScript:Names and VersionsJava Seri pris the programming language of the Web.The overwhelm ig majority ofmodern websites use JavaScript, and all modern web browsers on desktops, gameconsoles, tablets, and smartphones include JavaScript interpreters, making Java-Script the most ubiquitous programming language in history-JavaScript is part of thetriad of technologies that all Web developers must learn:HTML.to specify the contentof webpages, CSS to specify the presentation of webpages, and Java Scr pt to specifythe behavior of webpages.This book wil help you master the languageIf you are already familar with other programming languages, it may help you to knowthar Java Scrip risa high-level, dynamic, untyped interpreted programming languagethat is well-suited to object-oriented and functional programming styles.JavaScriptderives its syntax from Java, its first-class functions from Scheme, and its prototypebased inheritance from Self.But you do not need to know any of those languages, orbe familiar with those terms, to use this book and learn Java Scrip rThe name JavaScript”is actually somewhat misleading.Except for a superficial syn·tactic resemblance, JavaScript is completely different from the Java programming lan·guage.And JavaScript has long since outgrown its scripting-language roots to becomea robust and eff cient general-purpose language.The latest version of the language(seethe sidebar) defines new features for serious large-scale software development.JavaScript was created at Netscape in the early days of the Web, and technically, “Java-Scrip r is a trademark licensed from Sun Microsystems(now Oracle) used to describeNetscape's(now Mozilla's) implementation of the language.Netscape submitted thelanguage for standardization to ECMA-the European Computer Manufacturer'sAssociation—and because of trademark issues, the standardized version of the languagewas stuck with the awkward name“ECMAScript, For the same trademark reasons,Microsof's version of the language is formally known as'J Scrip i.”In practice, justabour everyone calls the language Java Seri pt This book uses the name“ECMA Seri pt only to refer to the language standard.For the last de eade, all web browsers have implemented version 3 of the ECMAScriptstandard and there has really been no need to think about version numbers the lan-guage standard was stable and browser implementations of the language were, for themost part, interoperable.Recently, an important new version of the language has beendeine das ECMAScript version 5 and, at the time of this wri ing, browsers are beginningto implement ir.This book covers all the new features of ECMASeript5aswellas allthe long-standing features of ECMA Seri pt 3.You'l sometimes see these language ver-sions abbreviated asES3andES 5, just as you'II sometimes see the name Java Seri prabbreviated as JS.When we're speaking of the language itself.the only version numbers that ate relevantare ECMAScript versions 3or 5.(Version 4ofECMASctipt was underdevelopmentfor years, but proved to be too ambitious and was never released) 5ome times, however,you llaboseealavaSenpc version number.such as JavaSeript15orTavaScnpr 1.8These are Mozilla's version numbers:version 1.5is basically ECMAScript 3, and laterversions include nonstandard language extensions(see Chapter II) .Finally, there arealso version numbers attached to particular Java Sen pt interpreters or“engines.Goo-gle calls its JavaScript interpreter V 8, for example, and at the time of this wi ing thecurrentversion of the V 8 engine is 3.0.To be useful every Language must have a platform or standard library or API off une-tions for performing things like basic input and output.The core JavaScript languagedefines a minimal API for working with text, arrays, dates, and regular expressions butdoes not include any input or output functionality Input and output(as well as moresophisticated t eatures, such as networking.storage, and graphics] are the responsibilityof the“host environment”within which JavaScript is embedded.Usually that hostenvironment is a webbrowser(though we'll see two uses of JavaScript without a webbrowser in Chapter 12) .Part I of this book covers the language itself and its minimalbuilt-in APL Part II explains how Java Scrip r is used in web browsers and covers thesprawling browser-based APIs loosely known as“client-side Java Scrip.”Part Ill is the reference section for the core API You can read abour the JavaScript arraymanipulation API by look ng up“Array in this part of the book, for examplePart IV is the reference section for client-side Java Scr pt.You might lookup“Canvas”

《JavaScript The Definitive Guide_David Flanagan_英文版》目录

1.Introduction to JavaScript..

Part l.

2.Lexical Structure.....

3.Types, Values, and Variables.

4.Expressions and Operators.


LI Core Java Scrip r

1.2 Client-Side JavaScript

Core JavaScript

2.1 Character Set


2.3 Literals

2.+Identifi ets and Reserved Words

2.5 Optional Semicolons

3.1 Numbers

3.2 Text

3.3 Boolean Values

3.4null and undefined

3.5 The Global Object

3.6 Wrapper Objects

6.1 Creating Objects

6.2 Querying and Seting Properties

6.3 Deleting Properties

6.4 Testing Properties

6.5 Enumerating Properties

6.6 Property Getters and Setters

6.7 Property Attributes


6.9 Serializing Objects

6.10Objecr Methods

7.1Creang Arrays

7.2 Reading and Writing Array Elements

7.3 Sparse Arrays

7.4 Array Length

7.5 Adding and Deleting Array Elements

7.6Ite rating Arrays

77Mulid mensional Arrays

7.8 Array Methods

7.9 ECMAScript 5 Array Methods

7.10 ArrayType

3.7ImmutablePnmitive Values and Mutable Object References

3.8 Type Conversions

3.9 Variable Declaration

3.10Van able Scope

4.1 Primary Expressions

4.2 Object and Array Initializers

4.3 Function Definition Expressions

4.4 Property Access Expressions

4.5 Invocation Expressions

4.6 Object Creation Expressions

4.7 Operator Overview

4.8 Arithmetic Expressions

4.9 Relational Expressions

4.10 Logical Expressions

4.11 Assignment Expressions

4.12 Evaluation Expressions

4.13 Miscellaneous Operators

5.1 Expression Statements

5.2CompoundandEmpry Statements

5.3 Declaration Statements

5.4 Conditionals



5.7 Miscellaneous Statements

5.8 Summary of JavaScript Statements



7.11 Array-Like Objects

7.12 Strings As Arrays

8.1 Defining Functions

8.2 Invoking Functions


9.Classes and Modules..

8.3 Function Arguments and Parameters

8.4 Functions As Values

8.5 Functions As Namespaces

8.6 Closures

87 Function Properties, Methods, and Constructor

8.8 Functional Programming

9.1 Classes and Prototypes

9.2 Classes and Constructors

9.3Java-Style Classes in JavaScript

9.4 Augmenting Classes

9.5 Classes and Types

96 Object-Oriented Techniques in Java Seri pr

9.7 Subclasses

9.8 Classes in ECMAScript 5

9.9 Modules

10.1 Defining Regular Expressions

10.2 String Methods for Pat tem Matching

10.3 The RegExp Object

11.1 JavaScript Subsets

11.2 Constants and Scoped Van i ables

11.3De structuring Assignment

11.4 Iteration

11.5 Shorthand Functions

11.6 Multiple Catch Clauses

11.7E4X:ECMA Seri pt for XML


12.2 Asynchronous I/O with Node

13.1Clent-Side JavaScript

13.2 Embedding JavaScript in HTML

13.3 Execution of Java Scrip r Programs

13.4Compatbity and Inter opera bily

13.5 Accessibility

13.6 Security

13.7 Client-Side Frameworks

14.1 Timers

14.2 Browser Location and Navigation

14.3 Browsing History

14.4 Browser and Screen Information

14.5 Dialog Boxes

14.6 Error Handing

14.7 Document Elements As Window Properties

14.8 Multiple Windows and Frames

15.1 Overview of the DOM

15.2 Selecting Document Elements

15.3 Document Structure and Traversal

15.4 Attributes

15.5 Element Content

15.6 Creating, Inserting, and Deleting Nodes

15.7 Example:Generating a TableofContents

15.8 Document and Element Geometry and Scrolling

15.9HTML Forms

15.10 Other Document Features

16.1 Overview of CSS

16.2 Important CSS Properties

16.3Scnpting Inline Styles

16.4 Querying Computed Styles

16.5 Scripting CSS Classes

16.6Scnpting Stylesheets

17.1 Types of Events

10.Pattern Matching with Regular Expressions.

11.JavaScript Subsets and Extensions..

12.Server-Side JavaScript..

Part II.Client-Side JavaScript

13.JavaScript in Web Browsers.

14.The Window Object..

15.Scripting Documents.....

16.Scripting CSS..

17.Handing Event..

17.2 Registering Event Handlers

173 EventHandler Invocation

17.4 Document Load Events

17.5 Mouse Events

17.6 Mousewheel Events

17.7Drag and Drop Events

17.8 Text Events

17.9 Keyboard Events

181UngXMLHep Request