javascript_cookbook.pdf

(13024 KB) Pobierz
JavaScript
Cookbook
PROGRAMMING THE WEB
Shelley Powers
2n
d
Ed
iti
on
JavaScript Cookbook
Problem solving with JavaScript is a lot trickier now that its use has expanded
considerably in size, scope, and complexity. This cookbook has your back,
with recipes for common tasks across the JavaScript world, whether you’re
working in the browser, the server, or a mobile environment. Each recipe
includes reusable code and practical advice for tackling JavaScript objects,
Node, Ajax, JSON, data persistence, graphical and media applications, complex
frameworks, modular JavaScript, APIs, and many related technologies.
Aimed at people who have some experience with JavaScript, the first part
covers traditional uses of JavaScript, along with new ideas and improved
functionality. The second part dives into the server, mobile development,
and a plethora of leading-edge tools. You’ll save time—and learn more
about JavaScript in the process.
Topics include:
Classic JavaScript:
Acomprehensive,example-
driventourofthelanguage
anditsplatforms.
author of
Speaking JavaScript
—Dr. Axel Rauschmayer
Arrays, functions, and the JavaScript Object
Accessing the user interface
Testing and accessibility
Creating and using JavaScript libraries
Client-server communication with Ajax
Rich, interactive web effects
JavaScript, All Blown Up:
New ECMAScript standard objects
Using Node on the server
Modularizing and managing JavaScript
Complex JavaScript frameworks
Advanced client-server communications
Visualizations and client-server graphics
Mobile application development
Shelley Powers
has been working
with and writing about web tech-
n o logie s—from th e fir st releas e
of JavaScript to the latest graphics
and design tools—for more than 18
years. Her recent O’Reilly books have
covered JavaScript, HTML5 media
objects, Ajax, and web graphics.
JAVA SCRIPT/ WEB
Twitter: @oreillymedia
facebook.com/oreilly
CAN $57.99
US $49.99
ISBN: 978-1-491-90188-5
SECOND EDITION
JavaScript Cookbook
Shelley Powers
JavaScript Cookbook, Second Edition
by Shelley Powers
Copyright © 2015 Shelley Powers. 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://safaribooksonline.com). For more information, contact our corporate/
institutional sales department: 800-998-9938 or
corporate@oreilly.com.
Editors:
Simon St. Laurent and Brian MacDonald
Production Editor:
Kara Ebrahim
Copyeditor:
Jasmine Kwityn
Proofreader:
Kara Ebrahim
February 2015:
Second Edition
Indexer:
Judy McConville
Cover Designer:
Ellie Volckhausen
Interior Designer:
David Futato
Illustrator:
Rebecca Demarest
Revision History for the Second Edition:
2015-01-23:
First release
See
http://oreilly.com/catalog/errata.csp?isbn=9781491901885
for release details.
The O’Reilly logo is a registered trademark of O’Reilly Media, Inc.
JavaScript Cookbook, Second Edition,
the
cover image of a little egret, and related trade dress are trademarks of O’Reilly Media, Inc.
While the publisher and the author have used good faith efforts to ensure that the information and instruc‐
tions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors
or omissions, including without limitation responsibility for damages resulting from the use of or reliance
on this work. Use of the information and instructions contained in this work is at your own risk. If any code
samples or other technology this work contains or describes is subject to open source licenses or the intel‐
lectual property rights of others, it is your responsibility to ensure that your use thereof complies with such
licenses and/or rights.
ISBN: 978-1-491-90188-5
[LSI]
Table of Contents
The World of JavaScript. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
Part I.
Classic JavaScript
3
7
10
14
16
18
22
25
25
29
31
32
33
36
37
39
40
43
45
46
48
iii
1. The JavaScript Not-So-Simple Building Blocks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1. Differentiating Between a JavaScript Object, Primitive, and Literal
1.2. Extracting a List from a String
1.3. Checking for an Existing, Nonempty String
1.4. Inserting Special Characters
1.5. Replacing Patterns with New Strings
1.6. Finding and Highlighting All Instances of a Pattern
1.7. Swapping Words in a String Using Capturing Parentheses
1.8. Replacing HTML Tags with Named Entities
1.9. Converting an ISO 8601 Formatted Date to a Date Object Acceptable
Format
1.10. Using Function Closures with Timers
1.11. Tracking Elapsed Time
1.12. Converting a Decimal to a Hexadecimal Value
1.13. Summing All Numbers in a Table Column
1.14. Converting Between Degrees and Radians
1.15. Find the Radius and Center of a Circle to Fit Within a Page Element
1.16. Calculating the Length of a Circular Arc
1.17. Using ES6 String Extras Without Leaving Users in the Dirt
2.1. Searching Through an Array
2.2. Flattening a Two-Dimensional Array with concat() and apply()
2.3. Removing or Replacing Array Elements
2.4. Extracting a Portion of an Array
2. JavaScript Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Zgłoś jeśli naruszono regulamin