OReilly Data Structures and Algorithms with JavaScript (2014).pdf

(8488 KB) Pobierz
Data Structures and Algorithms
with JavaScript
Michael McMillan
Data Structures and Algorithms with JavaScript
by Michael McMillan
Copyright © 2014 Michael McMillan. 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.
Editors:
Brian MacDonald and Meghan Blanchette
Production Editor:
Melanie Yarbrough
Copyeditor:
Becca Freed
Proofreader:
Amanda Kersey
Indexer:
Ellen Troutman-Zaig
March 2014:
First Edition
Cover Designer:
Karen Montgomery
Interior Designer:
David Futato
Illustrators:
Rebecca Demarest and Cynthia Clarke
Fehrenbach
Revision History for the First Edition:
2014-03-06:
First release
See
http://oreilly.com/catalog/errata.csp?isbn=9781449364939
for release details.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly
Media, Inc.
Data Structures and Algorithms with JavaScript,
the image of an amur hedgehog, 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-1-449-36493-9
[LSI]
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
1. The JavaScript Programming Environment and Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
The JavaScript Environment
JavaScript Programming Practices
Declaring and Intializing Variables
Arithmetic and Math Library Functions in JavaScript
Decision Constructs
Repetition Constructs
Functions
Variable Scope
Recursion
Objects and Object-Oriented Programming
Summary
JavaScript Arrays Defined
Using Arrays
Creating Arrays
Accessing and Writing Array Elements
Creating Arrays from Strings
Aggregate Array Operations
Accessor Functions
Searching for a Value
String Representations of Arrays
Creating New Arrays from Existing Arrays
Mutator Functions
Adding Elements to an Array
Removing Elements from an Array
1
2
3
3
4
6
7
8
10
10
12
13
13
14
15
15
16
17
17
18
18
19
19
20
2. Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
iii
Zgłoś jeśli naruszono regulamin