logo资料库

Head First JavaScript Programming 无水印原版pdf.pdf

第1页 / 共704页
第2页 / 共704页
第3页 / 共704页
第4页 / 共704页
第5页 / 共704页
第6页 / 共704页
第7页 / 共704页
第8页 / 共704页
资料共704页,剩余部分请下载后查看
Cover
Copyright
Table of Contents
Intro: How to use this book
Who is this book for?
We know what you're thinking.
Metacognition: thinking about thinking
Read Me
Tech Reviewers
Acknowledgments
1: Getting your feet wet
The way JavaScript works
How you're going to write JavaScript
How to get JavaScript into your page
JavaScript, you've come a long way baby...
How to make a statement
Variables and values
Back away from that keyboard!
Express yourself
How the while loop works
Making decisions with JavaScript
And, when you need to make LOTS of decisions
Reach out and communicate with your user
Who does what?
A closer look at console.log
Opening the console
Coding a Serious JavaScript Application
How do I add code to my page? (let me count the ways)
We're going to have to separate you two
Bullet points
2: Going further
Let's build a Battleship game
First, a high-level design
Working through the Pseudocode
Oh, before we go any further, don't forget the HTML!
Writing the Simple Battleship code
Now let's write the game logic
Step One: setting up the loop, getting some input
How prompt works
Checking the user's guess
So, do we have a hit?
Adding the hit detection code
Provide some post-game analysis
And that completes the logic!
Doing a little Quality Assurance
Can we talk about your verbosity...
Finishing the Simple Battleship game
How to assign random locations
Back to do a little more QA
Congrats on your first true JavaScript program, and a short word about reusing code
3: Getting functional
What's wrong with the code anyway?
By the way, did we happen to mention FUNCTIONS?
Okay, but how does it actually work?
What can you pass to a function?
JavaScript is pass-by-value.
Functions can return things too
Tracing through a function with a return statement
Global and local variables
Knowing the scope of your local and global variables
The short lives of variables
Don't forget to declare your locals!
Webville Guide to Code Hygiene
4: Arrays
Can you help Bubbles-R-Us?
How to represent multiple values in JavaScript
How arrays work
How to access an array item
How big is that array anyway?
The Phrase-O-Matic
Meanwhile, back at Bubbles-R-Us...
How to iterate over an array
But wait, there's a better way to iterate over an array
Test drive the bubble report
It's that time again...Can we talk about your verbosity?
Redoing the for loop with the post-increment operator
Creating an array from scratch (and adding to it)
Test drive the final report
A quick survey of the code...
Writing the printAndGetHighScore function
Refactoring the code using printAndGetHighScore
Putting it all together...
5: A trip to Objectville
Did someone say "Objects"?!
Thinking about properties...
How to create an object
What is Object-Oriented Anyway?
How properties work
How does a variable hold an object? Inquiring minds want to know...
Comparing primitives and objects
Doing even more with objects...
Doing some pre-qualification
Stepping through pre-qualification
Let's talk a little more about passing objects to functions
Putting Fido on a diet...
The Auto-O-Matic
Oh Behave! Or, how to add behavior to your objects
Improving the drive method
Take the fiat for a test drive
Why doesn't the drive method know about the started property?
A test drive with "this"
How this works
How behavior affects state...Adding some Gas-o-line
Now let's affect the behavior with the state
Congrats on your first objects!
Guess what? There are objects all around you! (and they'll make your life easier)
Crack the Code Challenge
6: Getting to know the DOM
So what does the code do?
A quick recap
How JavaScript really interacts with your page
How to bake your very own DOM
A first taste of the DOM
Getting an element with getElementById
What, exactly, am I getting from the DOM?
Finding your inner HTML
What happens when you change the DOM
A test drive around the planets
Don't even think about running my code until the page is fully loaded!
You say "event handler," I say "callback"
Why stop now? Let's take it further
How to set an attribute with setAttribute
More fun with attributes!
Meanwhile, back at the solar system...
So what else is a DOM good for anyway?
7: Serious types
The truth is out there...
Watch out, you might bump into undefined when you aren't expecting it...
How to use null
Dealing with NaN
We have a confession to make
Understanding the equality operator (otherwise known as ==)
How equality converts is operands (sounds more dangerous than it actually is)
How to get strict with equality
Even more type conversions...
How to determine if two objects are equal
The truthy is out there...
What JavaScript considers falsey
The Secret Life of Strings
How a string can look like a primitive and an object
A five-minute tour of string methods (and properties)
Chair Wars
8: Building an app
This time, let's build a REAL Battleship game
Stepping back...to HTML and CSS
Creating the HTML page: the Big Picture
Adding some more style
Using the hit and miss classes
How to design the game
Implementing the View
Implementing displayMessage
How displayHit and displayMiss work
Implementing displayHit and displayMiss
Another Test Drive...
The Model
You're gonna need a bigger boat...and game board
How we're going to represent the ships
Implementing the model object
Thinking about the fire method
Looking for hits
Putting that all together...
Wait, can we talk about your verbosity again?
Meanwhile back at the battleship...
A view to a kill...
Implementing the Controller
Processing the player's guess
Planning the code...
Implementing parseGuess
Meanwhile back at the controller...
Getting a player's guess
How to add an event handler to the Fire! button
Passing the input to the controller
What's left? Oh yeah, darn it, those hardcoded ships!
How to place ships
Generate the starting location for the new ship
Completing the generateShip method
Avoiding a collision!
Two final changes
Congrats, It's Startup Time!
9: Handling events
What are events?
What's an event handler?
How to create your first event handler
Test drive your event
Getting your head around events...by creating a game
Implementing the game
Let's add some more images
Now we need to assign the same event handler to each image's onclick property
How to reuse the same handler for all the the images
How the event object works
Putting the event object to work
Test drive the event object and target
Events and queues
Even more events
How setTimeout works
Finishing the image game
Test driving the timer
10: Liberated functions
The mysterious double life of the function keyword
Function declarations versus function expressions
Parsing the function declaration
What's next? The browser executes the code
Moving on...The conditional
And finishing up...
How functions are values too
Did we mention functions have First Class status in JavaScript?
Flying First Class
Writing code to process and check passengers
Iterating through the passengers
Passing a function to a function
Returning functions from functions
Writing the flight attendant drink order code
The flight attendant drink order code: a different approach
Wait, we need more drinks!
Taking orders with first class functions
Webville Cola
How the array sort method works
Putting it all together
Meanwhile back at Webville Cola
Take sorting for a test drive
11: Serious functions
Taking a look at the other side of functions...
How to use an anonymous function
We need to talk about your verbosity, again
When is a function defined? It depends...
What just happened? Why wasn't fly defined?
How to nest functions
How nesting affects scope
A little review of lexical scope
Where things get interesting with lexical scope
Functions Revisited
Calling a function (revisited)
What the heck is a closure?
Closing a function
Using closures to implement a magic counter
Test drive your magic counter
Creating a closure by passing a function expression as an argument
The closure contains the actual environment, not a copy
Creating a closure with an event handler
Click me! without a closure
Test drive your button counter
How the Click me! closure works
12: Creating objects
Creating objects with object literals
Using conventions for objects
Introducing Object Constructors
How to create a Constructor
How to use a Constructor
How constructors work
You can put methods into constructors as well
It's Production Time!
Let's test drive some new cars
Don't count out object literals just yet
Rewiring the arguments as an object literal
Reworking the Car constructor
Understanding Object Instances
Even constructed objects can have their own independent properties
Real World Constructors
The Array object
Even more fun with built-in objects
13: Extra strength objects
Hey, before we get started, we've got a better way to diagram our objects
Revisiting object constructors: we're reusing code, but are we being efficient?
Is duplicating methods really a problem?
What are prototypes?
Inheriting from a prototype
How inheritance works
Overriding the prototype
So where do you get a prototype?
How to set up the prototype
Test drive the prototype with some dogs
Give Spot his WOOF! in code
Teaching all dogs a new trick
Prototypes are dynamic
A more interesting implementation of the sit method
One more time: how the sitting property works
How to approach the design of the show dogs
Setting up a chain of prototypes
How inheritance works in a prototype chain
Creating the show dog prototype
First, we need an object that inherits from the dog prototype
Next, turning our dog instance into a show dog prototype
Now it's time to fill in the prototype
Creating a show dog instance
Test drive the show dog
Examining the exercise results
A final cleanup of show dogs
A little more cleanup
Stepping through Dog.call
The final test drive
The chain doesn't end at dog
Using inheritance to your advantage...by overriding built-in behavior
Using inheritance to your advantage...by extending a built-in object
Test driving the cliche machine
Grand Unified Theory of JavaScript
Appendix: The top ten topics (we didn't cover)
#1: jQuery
#2: Doing more with the DOM
#3: The WIndow Object
#4: Arguments
#5: Handling exceptions
#6: Adding event handlers with addEventListener
#7: Regular Expressions
#8: Recursion
#9: JSON
#10: Server-side JavaScript
Colophon
Index
Praise for Head First JavaScript Programming “Warning: Do not read Head First JavaScript Programming unless you want to learn the fundamentals of programming with JavaScript in an entertaining and meaningful fashion. There may be an additional side effect that you may actually recall more about JavaScript than after reading typical technical books.” — Jesse Palmer, Senior Software Developer, Gannett Digital “If every elementary and middle school student studied Elisabeth and Eric’s Head First HTML and CSS, and if Head First JavaScript Programming and Head First HTML5 Programming were part of the high school math and science curriculum, then our country would never lose its competitive edge.” — Michael Murphy, senior systems consultant, The History Tree “The Head First series utilizes elements of modern learning theory, including constructivism, to bring readers up to speed quickly. The authors have proven with this book that expert-level content can be taught quickly and efficiently. Make no mistake here, this is a serious JavaScript book, and yet, fun reading!” — Frank Moore, Web designer and developer “Looking for a book that will keep you interested (and laughing) but teach you some serious programming skills? Head First JavaScript Programming is it!” — Tim Williams, software entrepreneur “Add this book to your library regardless of your programming skill level!” — Chris Fuselier, engineering consultant “Robson and Freeman have done it again! Using the same fun and information-packed style as their previous books in the Head First series, Head First JavaScript Programming leads you through entertaining and useful projects that, chapter-by-chapter, allow programmers—even nonspecialists like myself—to develop a solid foundation in modern JavaScript programming that we can use to solve real problems.” — Russell Alleen-Willems, digital archeologist, DiachronicDesign.com “Freeman and Robson continue to use innovative teaching methods for communicating complex concepts to basic principles.” — Mark Arana, Strategy & Innovation, The Walt Disney Studios
Praise for other books by Eric T. Freeman and Elisabeth Robson “Just the right tone for the geeked-out, casual-cool guru coder in all of us. The right reference for practical development strategies—gets my brain going without having to slog through a bunch of tired, stale professor-speak.” — Travis Kalanick, CEO Uber “This book’s admirable clarity, humor and substantial doses of clever make it the sort of book that helps even non-programmers think well about problem-solving.” — Cory Doctorow, co-editor of Boing Boing, Science Fiction author “I feel like a thousand pounds of books have just been lifted off of my head.” — Ward Cunningham, inventor of the Wiki “One of the very few software books I’ve ever read that strikes me as indispensable. (I’d put maybe 10 books in this category, at the outside.)” — David Gelernter, Professor of Computer Science, Yale University “I laughed, I cried, it moved me.” — Daniel Steinberg, Editor-in-Chief, java.net “I can think of no better tour guides than Eric and Elisabeth.” — Miko Matsumura, VP of Marketing and Developer Relations at Hazelcast Former Chief Java Evangelist, Sun Microsystems “I literally love this book. In fact, I kissed this book in front of my wife.” — Satish Kumar “The highly graphic and incremental approach precisely mimics the best way to learn this stuff...” — Danny Goodman, author of Dynamic HTML: The Definitive Guide “Eric and Elisabeth clearly know their stuff. As the Internet becomes more complex, inspired construction of web pages becomes increasingly critical. Elegant design is at the core of every chapter here, each concept conveyed with equal doses of pragmatism and wit.” — Ken Goldstein, former CEO of Shop.com and author of This is Rage: A Novel of Silicon Valley and Other Madness
Other O’Reilly books by Eric T. Freeman and Elisabeth Robson Head First Design Patterns Head First HTML and CSS Head First HTML5 Programming Other related books from O’Reilly Head First HTML5 Programming JavaScript: The Definitive Guide JavaScript Enlightenment Other books in O’Reilly’s Head First series Head First HTML and CSS Head First HTML5 Programming Head First Design Patterns Head First Servlets and JSP Head First SQL Head First Software Development Head First C# Head First Java Head First Object-Oriented Analysis and Design (OOA&D) Head First Ajax Head First Rails Head First PHP & MySQL Head First Web Design Head First Networking Head First iPhone and iPad Development Head First jQuery
Head First JavaScript Programming Wouldn’t it be dreamy if there was a JavaScript book that was more fun than going to the dentist and more revealing than an IRS form? It’s probably just a fantasy... Eric T. Freeman Elisabeth Robson Beijing • Cambridge • Kln • Sebastopol • Tokyo
Head First JavaScript Programming by Eric T. Freeman and Elisabeth Robson Copyright © 2014 Eric Freeman, Elisabeth Robson. 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 Media 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. Meghan Blanchette, Courtney Nash Randy Comer Eric T. Freeman, Elisabeth Robson Melanie Yarbrough Potomac Indexing Rachel Monaghan Oliver Editors: Cover Designer: Code Monkeys: Production Editor: Indexer: Proofreader: Page Viewer: Printing History: March 2014: First Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. The Head First series designations, Head First JavaScript Programming, 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 the authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. In other words, if you use anything in Head First JavaScript Programming to, say, run a nuclear power plant, you’re on your own. We do, however, encourage you to visit Webville. No variables were harmed in the making of this book. ISBN: 978-1-449-34013-1 [M]
分享到:
收藏