JavaScript for PHP Developers
Stoyan Stefanov
Beijing Cambridge Farnham Kln Sebastopol Tokyo
Acknowledgments
Stoyan Stefanov
Los Angeles, April 2013
First and foremost, big thanks and gratitude to my second-time reviewers who helped me with JavaScript Patterns before, the Three Musketeers and dArtagnan: the ever-so-amazing Andrea Giammarchi, Asen Bozhilov, Dmitry Soshnikov, and Juriy kangax Zaytsev. As with the previous book, they helped me tremendously with their deep knowledge and experience with JavaScript, their attention to detail, and their determination to have technical accuracy above all. They would often comment on some of my bold general statements with: Hmm, this is not entirely correct and not always true, because. I am forever in debt to these four ridiculously talented developers who also happen to be great and friendly people.
Many thanks to Chris Shiflett and Sean Coates. They made sure the PHP side of things made sense, but whats more, this whole book started as a post on their PHPAdvent (now WebAdvent.org) blog, followed by a talk at the ConFoo conference, which Sean helps organize.
Next, thanks to the Facebook engineers who hang out in the JavaScript group. I posted an early draft there asking for comments. Three people even went through the whole thing and gave me invaluable feedback and further nitpicking, which is the best quality in a technical reviewer. Thanks to Alok Menghrajani, James Ide, and Alex Himel.
Finally, thanks to Randy Owens who read the early release of the book and meticulously filed tens of errata reports.
Dedication
To Nathalie, Zlatina, and Eva
Special Upgrade Offer
If you purchased this ebook directly from oreilly.com, you have the following benefits:
DRM-free ebooksuse your ebooks across devices without restrictions or limitations
Multiple formatsuse on your laptop, tablet, or phone
Lifetime access, with free updates
Dropbox syncingyour files, anywhere
If you purchased this ebook from another retailer, you can upgrade your ebook to take advantage of all these benefits for just $4.99. to access your ebook upgrade.
Please note that upgrade offers are not available from sample content.
Preface
JavaScript is the language of the browser. Its also the language that allows you to build increasingly diverse types of client, server, desktop, mobile, and shell applications. HTML5 continues the trend of allowing you to build rich applications (formerly known as RIA, Ajax, and DHTML) with new features such as local storage, databases on the client, geolocation, image, and audio and video APIsall exposed and scriptable with JavaScript. Fluency in JavaScript is a must for every web developer today.
This book is for the PHP developer whos learning JavaScript from the ground up. Both languages look deceptively close because they both share the similar C-like syntax. But they differ in many concepts, such as object creation, classes, and prototypes, which are critical to mastering each language. The book draws on your previous experience and so it wont put you to sleep explaining the most trivial topics such as conditions and loops. At the same time, the book is a complete reference: all the important information is here, emphasizing the differences with PHP and glossing over the similarities.
Conventions Used in This Book
The following typographical conventions are used in this book:
Italic Indicates new terms, URLs, email addresses, filenames, and file extensions. Constant width
Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords. Constant width bold
Shows commands or other text that should be typed literally by the user. Constant width italic
Shows text that should be replaced with user-supplied values or by values determined by context.
Tip
This icon signifies a tip, suggestion, or general note.
Warning
This icon indicates a warning or caution.
Using Code Examples
This book is here to help you get your job done. In general, if this book includes code examples, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless youre reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from OReilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your products documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: JavaScript for PHP Developers by Stoyan Stefanov (OReilly). Copyright 2013 Stoyan Stefanov, 978-1-449-32019-5.
If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .
Safari Books Online
Note
Safari Books Online is an on-demand digital library that delivers expert content in both book and video form from the worlds leading authors in technology and business.
Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training.
Safari Books Online offers a range of product mixes and pricing programs for organizations, government agencies, and individuals. Subscribers have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like OReilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and dozens more. For more information about Safari Books Online, please visit us online.
How to Contact Us
Please address comments and questions concerning this book to the publisher:
OReilly Media, Inc. |
1005 Gravenstein Highway North |
Sebastopol, CA 95472 |
800-998-9938 (in the United States or Canada) |
707-829-0515 (international or local) |
707-829-0104 (fax) |
We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://oreil.ly/javascript-php.
To comment or ask technical questions about this book, send email to .
For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.
Find us on Facebook: http://facebook.com/oreilly
Follow us on Twitter: http://twitter.com/oreillymedia
Watch us on YouTube: http://www.youtube.com/oreillymedia
Chapter 1. Introduction