Statistics
360
Views
65
Downloads
0
Donations
Uploader

高宏飞

Shared on 2025-07-16
Support
Share

No description

Tags
No tags
Language: 中文
File Format: PDF
File Size: 1.6 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
Are you an experienced programmer who wants to get started quickly in JavaScript and the HTML DOM? This is your book. Do you need encyclopedic knowledge of JavaScript and/or the HTML DOM? This book is not for you. Are you a novice who wants to learn to program? This book is not for you. In fact, this language is not for you. Get a good Python book. This book will get you programming in JavaScript as quickly as possible. In addition, it will provide you with a basic understand- ing of the Document Object Model, the massive data structure used to represent web pages. With these tools, you will be able to build interactive web pages. If you program in C++ or Java, there are parts of the book you can skip over because the JavaScript statements are exactly the same. These parts are clearly marked. JavaScript is the language—the only language—used by browsers. To create interactive web pages, you need to know both JavaScript and the DOM. This book will get you started. Quick JavaScript
(This page has no text content)
Quick JavaScript David Matuszek
First edition published 2023 by CRC Press 6000 Broken Sound Parkway NW, Suite 300, Boca Raton, FL 33487-2742 and by CRC Press 4 Park Square, Milton Park, Abingdon, Oxon, OX14 4RN CRC Press is an imprint of Taylor & Francis Group, LLC © 2023 David Matuszek Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use. The authors and publishers have attempted to trace the copyright holders of all material reproduced in this pub- lication and apologize to copyright holders if permission to publish in this form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may rectify in any future reprint. Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including pho- tocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission from the publishers. For permission to photocopy or use material electronically from this work, access www.copyright.com or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978.750-8400. For works that are not available on CCC please contact mpkbookspermissions@tandf.co.uk Trademark notice: Product or corporate names may be trademarks or registered trademarks and are used only for identification and explanation without intent to infringe. Library of Congress Cataloging-in-Publication Data Names: Matuszek, David L., author. Title: Quick JavaScript / David Matuszek. Description: First edition. | Boca Raton: CRC Press, [2023] | Series: Quick programing | Includes bibliographical references and index. Identifiers: LCCN 2022046807 | ISBN 9781032417578 (hbk) | ISBN 9781032417561 (pbk) | ISBN 9781003359609 (ebk) Subjects: LCSH: JavaScript (Computer program language) | Computer programming. Classification: LCC QA76.73.J39 M385 2023 | DDC 005.2/762--dc23/eng/20221202 LC record available at https://lccn.loc.gov/2022046807 ISBN: 978-1-032-41757-8 (hbk) ISBN: 978-1-032-41756-1 (pbk) ISBN: 978-1-003-35960-9 (ebk) DOI: 10.1201/9781003359609 Typeset in Minion by SPi Technologies India Pvt Ltd (Straive)
To all my students past, present, and future
(This page has no text content)
vii Contents Author, xiii Preface, xv Versions of JavaScript, xvii Chapter 1 ◾ Introduction 1 1.1 Hello, World! 1 1.2 JavaScript in the Browser 1 1.3 Browser Consoles 2 1.4 Running JavaScript 3 1.5 Getting Input 4 1.6 Producing Output 4 Chapter 2 ◾ JavaScript: The Bare Minimum 7 2.1 Comments 7 2.2 Data Types 8 2.2.1 Primitives 8 2.2.2 User-Defined Objects 9 2.2.3 Built-In Object Types 9 2.2.4 Arrays 9 2.2.5 Sets 11 2.2.6 Maps 11
viii    ◾   Contents 2.2.7 Dates 12 2.2.8 Regular Expressions 14 2.3 Identifiers 17 2.4 let and const 17 2.5 var 18 2.6 Operators 19 2.7 Equality and Identity 21 2.8 Conversions 22 2.9 Statements 22 2.9.1 Semicolons 22 2.9.2 Declarations 23 2.9.3 Function Definitions 24 2.9.4 Familiar Statements 25 2.9.4.1 Assignment Statements 26 2.9.4.2 Expressions 27 2.9.4.3 Compound Statements 27 2.9.4.4 If Statements 28 2.9.4.5 While Loops 29 2.9.4.6 Do-While Loops 30 2.9.4.7 Traditional for Loops 31 2.9.4.8 Scope in Loops 32 2.9.4.9 Switch Statements 33 2.9.4.10 Labeled Statements 35 2.9.4.11 Break Statements 35 2.9.4.12 Continue Statements 36 2.9.4.13 Return Statements 37 2.9.4.14 Empty Statements 38 2.9.5 JavaScript-Specific Statements 38 2.9.5.1 For/of 38
Contents    ◾   ix 2.9.5.2 For/in 39 2.9.5.3 Throw 39 2.9.5.4 Try-catch-finally 40 2.9.5.5 The with Statement 42 2.10 Example: Prime Numbers 42 2.11 Testing 43 2.11.1 The Mocha Test Framework 43 2.11.2 Testing with Chai 45 2.11.3 Testing Example 46 Chapter 3 ◾ JavaScript: In More Detail 49 3.1 Strict Mode 49 3.2 Identifiers 50 3.3 Destructuring 51 3.4 Data Types 53 3.4.1 Numbers 53 3.4.2 Strings 55 3.4.3 Booleans 56 3.4.4 Symbols 57 3.4.5 Arrays 59 3.4.6 Sparse Arrays 59 3.4.7 Sets 60 3.4.8 Maps 61 3.4.9 WeakMaps 62 3.4.10 Promises 63 3.4.11 Conversions 65 3.5 Math 67 3.6 Reserved Words 68 3.7 Good Operators 69
x    ◾   Contents 3.8 Operator Notes 71 3.9 Bad Operators 72 3.10 Functions 73 3.10.1 Defining Functions 73 3.10.2 Parameters and Arguments 75 3.10.3 Functions Are Data 77 3.10.4 Functions Are Objects 78 3.10.5 Function Methods 79 3.10.6 Closures 80 3.10.7 Generators 82 3.10.8 Iterators 83 3.11 Objects 85 3.11.1 Definition of Objects 85 3.11.2 Creating Objects 87 3.11.3 Copying Objects 90 3.11.4 Methods 91 3.11.5 Optional Chaining 91 3.11.6 This 92 3.11.7 Higher-Order Functions 93 3.11.8 Prototypes 95 3.11.9 Descriptors 96 3.11.10 Classes and Inheritance 98 3.11.10.1 Classes 98 3.11.10.2 Inheritance 102 3.11.10.3 Overriding Methods and Fields 105 3.11.10.4 Class Prototypes 106 3.12 Transpilers and Polyfills 107 3.13 JSON 108
Contents    ◾   xi Chapter 4 ◾ Client-Side JavaScript 109 4.1 Essential HTML 109 4.2 Adding JavaScript to HTML 110 4.3 DOM Overview 111 4.4 Graphical User Interfaces 113 4.4.1 Events 113 4.4.2 Widgets 113 4.4.3 Buttons 114 4.4.4 Finding Widgets 115 4.4.5 Text Fields 116 4.4.6 Buttons and Forms 117 4.4.7 Form Verification 120 4.4.8 Form Submission 121 4.4.8.1 Get Requests 121 4.4.8.2 Post Requests 122 4.4.8.3 Other Requests 122 4.4.9 Additional Text Widgets 122 4.4.10 Other Input Widgets 124 4.4.11 Events 125 4.4.12 Bubbling 129 4.5 Using the DOM 129 4.5.1 The Window Object 129 4.5.1.1 Window Properties 130 4.5.1.2 Window Methods 131 4.5.1.3 Window Example 132 4.5.2 The Document Object 134 4.5.2.1 Document Properties 134 4.5.2.2 Finding Nodes 135 4.5.2.3 Creating Nodes 137
xii    ◾   Contents 4.5.3 Node Objects 138 4.5.3.1 Node Properties 138 4.5.3.2 Node Methods 139 4.5.4 Elements 140 4.5.4.1 Element Properties 140 4.5.4.2 Element Methods 140 4.5.5 CharacterData 142 4.5.6 Example: ShowTree 142 ◾ Afterword 147 Appendix a: Array Methods, 149 Appendix B: Higher-Order Methods, 151 Appendix C: String Methods, 153 Appendix D: Regular Expressions, 157 References, 161 Index, 163
xiii Author I ’m David Matuszek, known to most of my students as “Dr. Dave.” I wrote my first program on punched cards in 1963 and immedi- ately got hooked. I taught my first computer classes in 1970 as a graduate student in computer science at the University of Texas at Austin. I eventually earned a PhD there, and I’ve been teaching ever since. Admittedly, I spent over a dozen years in industry, but even then I taught as an adjunct for Villanova University. I finally escaped from industry and joined the Villanova fac- ulty full time for a few years, and then moved to the University of Pennsylvania, where I directed a master’s program (MCIT, Masters in Computer and Information Technology) for students coming into computer science from another discipline. Throughout my career, my main interests have been in artificial intelligence (AI) and programming languages. I’ve used a lot of programming languages. I retired in 2017, but I can’t stop teaching, so I’m writing a series of “quick start” books on programming and programming lan- guages. I’ve also written two science fiction novels, Ice Jockey and All True Value, and I expect to write more. Check them out!
xiv    ◾   Author If you found this book useful, it would be wonderful if you would post a review. Reviews, even critical ones, help to sell books. And, hey, if you’re a former student or colleague, drop me a note at david.matuszek@gmail.com. I’d love to hear from you!
xv Preface If you are a programmer who wants to get started programming in JavaScript as quickly as possible, this book is for you. If you are hoping to learn JavaScript as your first programming language, this book is not for you. If you want a comprehensive encyclopedia of JavaScript, this book is not for you. For that, let me recommend the excellent JavaScript: The Definitive Guide (David Flanagan, O’Reilly).
(This page has no text content)
xvii Versions of JavaScript JavaScript is an evolving language. It is defined by a series of standards known as ECMAScript by Ecma International. As a language, JavaScript looks much like Java or C++. Unlike these languages, the original version of JavaScript is widely regarded as a hastily written, poorly designed language. Because of this perception, other languages have been written for use on HTML pages (CoffeeScript, Dart, etc.), but these must be com- piled to JavaScript in order to be used by browsers. JavaScript is the only language understood by browsers. In order to make significant changes to the JavaScript language without breaking existing programs, ECMAScript 2015 intro- duced strict mode. Strict mode eliminates or modifies the worst parts of JavaScript. All new programming should be done in strict mode. To invoke strict mode for an entire program, put "use strict"; (complete with quotes) at the first line of a program. To invoke strict mode for a single function, put that string as the first line inside the function. The primary use of JavaScript is to add interactivity to web pages. Web pages are written in HTML, HyperText Markup Language. This book is not about HTML, but it does cover the minimum necessary.
xviii    ◾   Versions of JavaScript When running in a browser, JavaScript cannot do arbitrary file I/O, nor can it communicate with web servers other than the one from which it came. This is necessary for security; you do not want web pages to access your files without your knowledge. Other environments, such as IDEs, may allow these operations.
1DOI: 10.1201/9781003359609-1 1.1 HELLO, WORLD! Traditionally, the first program to be written in a new language is one that prints out “Hello, World!” We will follow that tradition. The one line of JavaScript needed is: alert("Hello, World!"); To execute this code, we have several options. • Put it in a web page and run it in a browser. • Run it in an IDE (Integrated Development Environment). • Run it from an editor application. • Run it from an online JavaScript editor. 1.2 JAVASCRIPT IN THE BROWSER If you save the following text in a file and then open the file in a browser, an alert dialog containing the text “Hello, World!” will appear. C H A P T E R 1 Introduction
The above is a preview of the first 20 pages. Register to read the complete e-book.