Fundamental
George Nicolas
An Introduction to JavaScript Programming
JavaScript
The Crypt: a running example
Throughout this book, you develop a text-based adventure game called The Crypt.
Players can explore locations on a map, moving from place to place and picking up
items to help them solve challenges and get past obstacles. The last section of each
chapter uses what you’ve learned to develop the game further. You’ll see how the pro-
gramming concepts help you build the pieces that are then combined to produce a
large program.
Game element Task JavaScript Chapter
Players Deciding what information you need to
know about each player
Variables 2
Collecting player information in one place Objects 3
Displaying information about players on
the console
Functions 4–7
Creating a list of items collected by each
player
Arrays 8
Organizing player-creation code Constructors 9
Places Creating lots of places to explore, all with
a similar structure
Constructors 9
Joining places with exits Square bracket notation 10
Game Adding simple functions for movement,
collecting items, and displaying informa-
tion
Square bracket notation 10
Maps Joining places with exits Square bracket notation 10
P
ART
1 C
ORE
CONCEPTS
ON
THE
CONSOLE
. .............................1
1
■
Programming, JavaScript, and JS Bin 3
2
■
Variables: storing data in your program 16
3
■
Objects: grouping your data 27
4
■
Functions: code on demand 40
5
■
Arguments: passing data to functions 57
6
■
Return values: getting data from functions 70
7
■
Object arguments: functions working with objects 83
8
■
Arrays: putting data into lists 104
9
■
Constructors: building objects with functions 122
10
■
Bracket notation: flexible property names 147
P
ART
2 O
RGANIZING
YOUR
PROGRAMS
..................................169
11
■
Scope: hiding information 171
12
■
Conditions: choosing code to run 198
13
■
Modules: breaking a program into pieces 221
14
■
Models: working with data 248
15
■
Views: displaying data 264
16
■
Controllers: linking models and views 280
P
ART
3 J
AVA
S
CRIPT
IN
THE
BROWSER
.....................................299
17
■
HTML: building web pages 301
18
■
Controls: getting user input 323
19
■
Templates: filling placeholders with data 343
20
■
XHR: loading data 367
21
■
Conclusion: get programming with JavaScript 387
22
■
Node: running JavaScript outside the browser online
23
■
Express: building an API online
24
■
Polling: repeating requests with XHR online
25
■
Socket.IO: real-time messaging online
contents
P
ART
1 C
ORE
CONCEPTS
ON
THE
CONSOLE
. ..................1
1
Programming, JavaScript, and JS Bin 3
1.1 Programming 3
1.2 JavaScript 4
1.3 Learning by doing and thinking 5
1.4 JS Bin 5
JS Bin panels 6
■
Following the code listings on JS Bin 7
Logging to the console 8
■
Code comments 9
■
Further
Adventures 9
■
Error messages 9
■
Line numbers 10
Get an account 10
1.5 The Crypt—our running example 11
Playing The Crypt 11
■
Steps for building The Crypt 12
1.6 Further examples and practice 14
1.7 Browser support 15
1.8 Summary 15