JavaScript Fundamental An Introduction to JavaScript Programming George Nicolas 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 youve learned to develop the game further. Youll 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 Variables know about each player Collecting player information in one place Objects Displaying information about players on Functions the console Creating a list of items collected by each Arrays player Organizing player-creation code Constructors Places Creating lots of places to explore, all with Constructors a similar structure Joining places with exits Square bracket notation Game Adding simple functions for movement, Square bracket notation collecting items, and displaying informa tion Maps Joining places with exits Square bracket notation
PART 1CORE CONCEPTS ON THE CONSOLE ..............................11 Programming, JavaScript, and JS Bin
2 Variables: storing data in your program
3 Objects: grouping your data
4 Functions: code on demand
5 Arguments: passing data to functions
6 Return values: getting data from functions
7 Object arguments: functions working with objects
8 Arrays: putting data into lists
9 Constructors: building objects with functions
10 Bracket notation: flexible property names
PART 2
ORGANIZING YOUR PROGRAMS ..................................16911 Scope: hiding information
12 Conditions: choosing code to run
13 Modules: breaking a program into pieces
14 Models: working with data
vvi15 Views: displaying data
16 Controllers: linking models and views
PART 3
JAVASCRIPT IN THE BROWSER.....................................29917 HTML: building web pages
18 Controls: getting user input
19 Templates: filling placeholders with data
20 XHR: loading data
21 Conclusion: get programming with JavaScript
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
contentsPART 1 CORE CONCEPTS ON THE CONSOLE...................11 Programming, JavaScript, and JS Bin 3 1.1 Programming 1.2 JavaScript 1.3 Learning by doing and thinking 1.4 JS Bin
JS Bin panels6Following the code listings on JS Bin7Logging to the console8Code comments9FurtherAdventures9Error messages9Line numbers10Get an account10 1.5 The Cryptour running example
Playing The Crypt11Steps for building The Crypt12 1.6 Further examples and practice 1.7 Browser support 1.8 Summary
vii CONTENTS
viii2 Variables: storing data in your program 16 2.1 What is a variable? 2.2 Declaring variables and assigning values
Declaring variables17Assigning values to variables18One-step declaration and assignment20Using a variable inits own assignment21 2.3 Choosing good variable names
Keywords and reserved words22Rules for namingvariables23camelCase23Use descriptivevariable names24 2.4 The Cryptplayer variables 2.5 Summary
3 Objects: grouping your data 27 3.1 A need for organization 3.2 Creating objects
Creating an empty object30Properties as key-value pairs30 3.3 Accessing object properties 3.4 Updating object properties 3.5 Further examples
Writing a blog35Creating a calendar35Whats theweather like?36The testing effect37Create your own37 3.6 The Crypta player object 3.7 Summary
4 Functions: code on demand 40 4.1 Noticing repetition
Displaying object properties as text41Adding tax anddisplaying a summary42 4.2 Defining and calling functions
Defining new functions44Function expressions andfunction declarations45Using functions46Functions step by step47 4.3 Reducing repetition
A function for displaying object properties as text48Functions for adding tax and displaying a summary50 CONTENTS
ix 4.4 Making code easier to read and update
Updating the showMovieInfo function52 4.5 The Cryptdisplaying player information
A function to display player information55 4.6 Summary
5 Arguments: passing data to functions 57 5.1 Function reuse and versatility 5.2 Passing information to functions
Passing one argument to a function59Passing multiplearguments to a function63 5.3 The Cryptdisplaying player information
Displaying players names65Displaying players health66Displaying players locations67Putting it all togetherdisplaying players information68 5.4 Summary
6 Return values: getting data from functions 70 6.1 Returning data from functions
The return value replaces the function call71The returnkeyword72Using arguments to determine the return value72 6.2 Experimenting at the console prompt
Calling functions75Declaring new variables76 6.3 The Cryptbuilding player information strings
Building strings for a players name, health, and location78A function for player informationputting the pieces together79 6.4 Summary
7 Object arguments: functions working with objects 83 7.1 Using objects as arguments
Accessing properties of an object argument84Adding propertiesto an object argument85 7.2 Returning objects from functions
Building planetsan object creation function87Points in2D space89 CONTENTS
x 7.3 Methodssetting functions as properties of objects
Namespacesorganizing related functions91Math methods92String methods94spacermore methodsfor your namespace96Deep namespace exploration98 7.4 The Cryptplayer objects as arguments 7.5 Summary
8 Arrays: putting data into lists 104 8.1 Creating arrays and accessing elements
Creating an array105Accessing array elements106 8.2 Array methods
Adding and removing elements111Slicing and splicingarrays111Visiting each element with forEach113 8.3 The Crypta player items array 8.4 Summary
9 Constructors: building objects with functions 122 9.1 Using functions to build objects
Adding properties124Adding methods125 9.2 Using constructor functions to build objects
Constructor functions127World buildingmaking use ofthe Planet constructor130Telling objects apart with theinstanceof operator131 9.3 Building masterytwo examples of constructors 9.4 The Cryptproviding places to plunder
Building the Place constructortitle and descriptionNext page