ebook img

Get Programming with javascript PDF

436 Pages·2016·11.66 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Get Programming with 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 Variables 2 know about each player Collecting player information in one place Objects 3 Displaying information about players on Functions 4–7 the console Creating a list of items collected by each Arrays 8 player Organizing player-creation code Constructors 9 Places Creating lots of places to explore, all with Constructors 9 a similar structure Joining places with exits Square bracket notation 10 Game Adding simple functions for movement, Square bracket notation 10 collecting items, and displaying informa- tion Maps Joining places with exits Square bracket notation 10 Get Programming with JavaScript Get Programming with JavaScript JOHN R. LARSEN MANNING SHELTER ISLAND For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: [email protected] ©2016 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. Development editor: Helen Stergius 20 Baldwin Road Technical development editors: Chuck Henderson, Ozren Harlovic PO Box 761 Review editor: Ozren Harlovic Shelter Island, NY 11964 Project editor: Tiffany Taylor Copyeditor: Linda Recktenwald Proofreaders: Elizabeth Martin Bonnie Culverhouse Technical proofreader: Romin Irani Typesetter: Dennis Dalinnik Cover designer: Leslie Haimes ISBN: 9781617293108 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – EBM – 21 20 19 18 17 16 brief contents PART 1 CORE 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 PART 2 ORGANIZING 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 v vi BRIEF CONTENTS 15 ■ Views: displaying data 264 16 ■ Controllers: linking models and views 280 PART 3 JAVASCRIPT 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 foreword xvii preface xix acknowledgments xxi about this book xxii PART 1 CORE 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 vii viii CONTENTS 2 Variables: storing data in your program 16 2.1 What is a variable? 17 2.2 Declaring variables and assigning values 17 Declaring variables 17 ■ Assigning values to variables 18 One-step declaration and assignment 20 ■ Using a variable in its own assignment 21 2.3 Choosing good variable names 22 Keywords and reserved words 22 ■ Rules for naming variables 23 ■ camelCase 23 ■ Use descriptive variable names 24 2.4 The Crypt—player variables 24 2.5 Summary 25 3 Objects: grouping your data 27 3.1 A need for organization 28 3.2 Creating objects 29 Creating an empty object 30 ■ Properties as key-value pairs 30 3.3 Accessing object properties 32 3.4 Updating object properties 33 3.5 Further examples 35 Writing a blog 35 ■ Creating a calendar 35 ■ What’s the weather like? 36 ■ The testing effect 37 ■ Create your own 37 3.6 The Crypt—a player object 37 3.7 Summary 39 4 Functions: code on demand 40 4.1 Noticing repetition 40 Displaying object properties as text 41 ■ Adding tax and displaying a summary 42 4.2 Defining and calling functions 43 Defining new functions 44 ■ Function expressions and function declarations 45 ■ Using functions 46 Functions step by step 47 4.3 Reducing repetition 48 A function for displaying object properties as text 48 Functions for adding tax and displaying a summary 50

Description:
Get Programming with javascript is a hands-on introduction to programming for readers who have never programmed. Youll be writing your own web apps, games, and programs in no time! Foreword by Remy Sharp.Are you ready to start writing your own web apps, games, and programs? You’re in the right pla
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.