The Book of JavaScript
Michael J. Ross writes "Developers of Web sites, whether professional programmers or Web hobbyists, are oftentimes impressed by the more advanced functionality that can be achieved on a Web page using JavaScript. Yet these personal discoveries of JavaScript's power do not always motivate the developers to implement similar functionality on their own sites — partly because most of those custom-built JavaScript functions are cryptic, and make no attempt to show how the developer would customize the code for their own use. The majority of JavaScript books are of limited help in this regard, because they focus on the language details, illustrated only with code snippets. Many readers would benefit more from instruction via working examples, which is the approach used by The Book of JavaScript." Read the rest of Michael's review.
The Book of JavaScript
author
David Thau
pages
528
publisher
No Starch Press
rating
8
reviewer
Michael J. Ross
ISBN
1886411360
summary
A guide for beginners to learn JavaScript via examples
The book was written by David Thau, a.k.a. "thau!," a veteran of the Internet and computer programming — especially JavaScript, which he has taught to countless other programmers, through this book, as well as numerous courses, conference presentations, and the tutorials he has written for Webmonkey.
Published by No Starch Press, the second edition of The Book of JavaScript appeared in December 2006. The publisher maintains a Web page for the book, where visitors can find an online copy of the book's table of contents, excerpts from reader reviews, links for purchasing the paper and electronic versions of the book, a sample chapter ("Chapter 2: Using Variables and Built-In Functions to Update Your Web Pages Automatically") as a PDF file, the errata discovered by the author and readers (several reported so far), and a link to the author's companion site.
Unlike some books' companion sites, this one is worth a visit, and not just to see a working example of the tip box described in chapter 8, which is used to show how to create an array. Thau's site has links for viewing, running, and downloading all of the sample code in the book's figures. In addition, the visitor can obtain copies of the book's JavaScript libraries, for doing cookies, form validation, and plug-in detection. There are links for viewing and downloading the three sample Web sites. However, in his AntWeb site, at least as of this writing, none of the images are showing up on his homepage. Another problem, of much less importance, is the strange behavior of the "Websites" and "Freeware" options on his "Chapters" drop-down list box. Choosing either one takes the visitor to his "Websites" page, but always displays "Freeware" in the drop-down. It is hard to imagine that this behavior is intentional.
The book's material spans 528 pages, most of which is found in 18 chapters, covering a variety of topics: an overview of JavaScript's capabilities, alternatives, and limitations, among other less technical issues; variables and built-in functions; browser detection and simple control flow; rollovers, events, images, and the "document" object; window manipulation and properties; creating your own JavaScript functions; Web forms; arrays and iteration; timing events; frames and image maps; handling form input and strings; cookies; dynamic HTML, CSS, and DOM; AJAX basics; XML; server-side AJAX; a sample application (an online to-do list); and debugging JavaScript and AJAX. The book also offers four appendices: answers to chapter assignments; online resources (tutorials, sample code, AJAX sites, and AJAX frameworks); a complete reference to JavaScript's objects and functions; the code for the book's two longest applications, namely, the Italian translator and the to-do list.
This second edition serves as a follow-up to the well-regarded first edition that came out seven years earlier. Both the book and the language itself have clearly withstood the test of time. There are several reasons for the popularity of that first edition: It taught the language and its capabilities largely through the use of complete JavaScript functions, each of which served as an illustrative example of not just the elements and rules of the language, but also straightforward ways of accomplishing common tasks in JavaScript. After all, looking at sample code is how most programmers prefer to learn or verify how a language works.
The book assumed no prior knowledge of JavaScript or any other computer language, on the part of the reader. This characteristic not only set it apart from the large number of other JavaScript titles published at that time, but it made the book more attractive to people new to programming in general and Web programming in particular. Such readers might also favor this book over others because of the author's approachable writing style, in which he fully explains topics in a leisurely manner, without the terseness seen in most programming language books. This is not to say that brevity in technical works is ever a mistake per se; the busy professional programmer wants to find answers as quickly as possible. But such brevity can quickly prove frustrating to non-techies, who lack the background for understanding terse explanations and for knowing where they can turn for clarification.
All of these laudable attributes of the first edition have been carried over into this latest edition. The primary change found in this second edition, is the coverage of AJAX (asynchronous JavaScript combined with XML). Even though the additional material substantially increases the length of the book, by 124 pages, the end result is still far from unwieldy — mostly due to several factors: The book's table of contents, along with the index, are detailed enough to make it relatively easy to find a particular topic in the book, assuming that it is included. The subsection listings in the table of contents, like good source code, make liberal use of whitespace for readability. In the text itself, coloring the headings and note numbers blue make them stand out.
Aside from the aforesaid problems with the book's companion site, there are a few other areas for improvement: It was noted earlier that the last appendix contains the sample code from chapters 15 and 17. The author states that these code listings were located in an appendix, rather than the chapters themselves, because they are too long. Actually, they comprise only a dozen pages, which would have been better located in the chapters where the reader expects to see them, and where they would be close at hand for referencing. The first listing is only two pages long, and definitely should be located in chapter 15. Even for the second listing, if the author is concerned about readers getting frustrated flipping through the 10 pages to find the continuation of the chapter's discussion, a simple note at the beginning of that code, as to what page the discussion is resumed, would be sufficient.
Some fundamental language elements of JavaScript are introduced fairly late in the book. For instance, it is noted above that an explanation as to how to create an array — an essential concept in just about any procedural language — is not found until the eighth chapter, on page 134. This is more than one third of the way into the book's 18 chapters. On the other hand, given that the author has chosen to present these language concepts, for the most part, only when needed and when describing the sample code, this later introduction of some key concepts might not prove much of a problem to most readers. However, this is a case in which the completeness and accuracy of the book's index, are even more critical than usual. In this regard, the book does not fail the reader, as the index appears to provide enough coverage.
The formatting of the code throughout the text is not entirely consistent, as evidenced by some open braces appearing on their own lines, thus wasting space, and in other cases on the same line as the preceding parenthesized expression, though separated by a pointlessly large number of spaces. Code level blocks are indented two or four spaces, seemingly at random. Continuation lines are indented exactly the same; they would be more clear if they had double the number of spaces as code level blocks. Of more importance to the reader attempting to figure out what code is serving what purpose, there are far too many large chunks of code lacking any comments, which are needed, since much of the code is not self-describing. In most of the functions, the only comment lines are those for hiding the JavaScript from outdated browsers — a practice that should have been phased out in this latest edition.
Any experienced programmer who needs a complete JavaScript reference book, or a book that covers all the language's elements in fine detail, would be best served by choosing a different book from this one. On the other hand, once they had secured such a book, they would likely find David Thau's contribution an enjoyable source of ideas on what can be done using JavaScript capabilities. For anyone who wishes to learn JavaScript in a practical and relaxed way, by reading clearly explained sample projects and their code, should be well pleased with The Book of JavaScript.
Michael J. Ross is a Web programmer, freelance writer, and the editor of PristinePlanet.com's free newsletter. He can be reached at www.ross.ws, hosted by SiteGround.
You can purchase The Book of JavaScript from amazon.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.
The book was written by David Thau, a.k.a. "thau!," a veteran of the Internet and computer programming — especially JavaScript, which he has taught to countless other programmers, through this book, as well as numerous courses, conference presentations, and the tutorials he has written for Webmonkey.
Published by No Starch Press, the second edition of The Book of JavaScript appeared in December 2006. The publisher maintains a Web page for the book, where visitors can find an online copy of the book's table of contents, excerpts from reader reviews, links for purchasing the paper and electronic versions of the book, a sample chapter ("Chapter 2: Using Variables and Built-In Functions to Update Your Web Pages Automatically") as a PDF file, the errata discovered by the author and readers (several reported so far), and a link to the author's companion site.
Unlike some books' companion sites, this one is worth a visit, and not just to see a working example of the tip box described in chapter 8, which is used to show how to create an array. Thau's site has links for viewing, running, and downloading all of the sample code in the book's figures. In addition, the visitor can obtain copies of the book's JavaScript libraries, for doing cookies, form validation, and plug-in detection. There are links for viewing and downloading the three sample Web sites. However, in his AntWeb site, at least as of this writing, none of the images are showing up on his homepage. Another problem, of much less importance, is the strange behavior of the "Websites" and "Freeware" options on his "Chapters" drop-down list box. Choosing either one takes the visitor to his "Websites" page, but always displays "Freeware" in the drop-down. It is hard to imagine that this behavior is intentional.
The book's material spans 528 pages, most of which is found in 18 chapters, covering a variety of topics: an overview of JavaScript's capabilities, alternatives, and limitations, among other less technical issues; variables and built-in functions; browser detection and simple control flow; rollovers, events, images, and the "document" object; window manipulation and properties; creating your own JavaScript functions; Web forms; arrays and iteration; timing events; frames and image maps; handling form input and strings; cookies; dynamic HTML, CSS, and DOM; AJAX basics; XML; server-side AJAX; a sample application (an online to-do list); and debugging JavaScript and AJAX. The book also offers four appendices: answers to chapter assignments; online resources (tutorials, sample code, AJAX sites, and AJAX frameworks); a complete reference to JavaScript's objects and functions; the code for the book's two longest applications, namely, the Italian translator and the to-do list.
This second edition serves as a follow-up to the well-regarded first edition that came out seven years earlier. Both the book and the language itself have clearly withstood the test of time. There are several reasons for the popularity of that first edition: It taught the language and its capabilities largely through the use of complete JavaScript functions, each of which served as an illustrative example of not just the elements and rules of the language, but also straightforward ways of accomplishing common tasks in JavaScript. After all, looking at sample code is how most programmers prefer to learn or verify how a language works.
The book assumed no prior knowledge of JavaScript or any other computer language, on the part of the reader. This characteristic not only set it apart from the large number of other JavaScript titles published at that time, but it made the book more attractive to people new to programming in general and Web programming in particular. Such readers might also favor this book over others because of the author's approachable writing style, in which he fully explains topics in a leisurely manner, without the terseness seen in most programming language books. This is not to say that brevity in technical works is ever a mistake per se; the busy professional programmer wants to find answers as quickly as possible. But such brevity can quickly prove frustrating to non-techies, who lack the background for understanding terse explanations and for knowing where they can turn for clarification.
All of these laudable attributes of the first edition have been carried over into this latest edition. The primary change found in this second edition, is the coverage of AJAX (asynchronous JavaScript combined with XML). Even though the additional material substantially increases the length of the book, by 124 pages, the end result is still far from unwieldy — mostly due to several factors: The book's table of contents, along with the index, are detailed enough to make it relatively easy to find a particular topic in the book, assuming that it is included. The subsection listings in the table of contents, like good source code, make liberal use of whitespace for readability. In the text itself, coloring the headings and note numbers blue make them stand out.
Aside from the aforesaid problems with the book's companion site, there are a few other areas for improvement: It was noted earlier that the last appendix contains the sample code from chapters 15 and 17. The author states that these code listings were located in an appendix, rather than the chapters themselves, because they are too long. Actually, they comprise only a dozen pages, which would have been better located in the chapters where the reader expects to see them, and where they would be close at hand for referencing. The first listing is only two pages long, and definitely should be located in chapter 15. Even for the second listing, if the author is concerned about readers getting frustrated flipping through the 10 pages to find the continuation of the chapter's discussion, a simple note at the beginning of that code, as to what page the discussion is resumed, would be sufficient.
Some fundamental language elements of JavaScript are introduced fairly late in the book. For instance, it is noted above that an explanation as to how to create an array — an essential concept in just about any procedural language — is not found until the eighth chapter, on page 134. This is more than one third of the way into the book's 18 chapters. On the other hand, given that the author has chosen to present these language concepts, for the most part, only when needed and when describing the sample code, this later introduction of some key concepts might not prove much of a problem to most readers. However, this is a case in which the completeness and accuracy of the book's index, are even more critical than usual. In this regard, the book does not fail the reader, as the index appears to provide enough coverage.
The formatting of the code throughout the text is not entirely consistent, as evidenced by some open braces appearing on their own lines, thus wasting space, and in other cases on the same line as the preceding parenthesized expression, though separated by a pointlessly large number of spaces. Code level blocks are indented two or four spaces, seemingly at random. Continuation lines are indented exactly the same; they would be more clear if they had double the number of spaces as code level blocks. Of more importance to the reader attempting to figure out what code is serving what purpose, there are far too many large chunks of code lacking any comments, which are needed, since much of the code is not self-describing. In most of the functions, the only comment lines are those for hiding the JavaScript from outdated browsers — a practice that should have been phased out in this latest edition.
Any experienced programmer who needs a complete JavaScript reference book, or a book that covers all the language's elements in fine detail, would be best served by choosing a different book from this one. On the other hand, once they had secured such a book, they would likely find David Thau's contribution an enjoyable source of ideas on what can be done using JavaScript capabilities. For anyone who wishes to learn JavaScript in a practical and relaxed way, by reading clearly explained sample projects and their code, should be well pleased with The Book of JavaScript.
Michael J. Ross is a Web programmer, freelance writer, and the editor of PristinePlanet.com's free newsletter. He can be reached at www.ross.ws, hosted by SiteGround.
You can purchase The Book of JavaScript from amazon.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.
Javascript: It's Not Just for Annoying Spammy Popup Ads Anymore!
Gamingmuseum.com: Give your 3D accelerator a rest.
AJAX is not "asynchronous JavaScript combined with XML" - 'cause, then, that would be AJCWX, and that is totally unpronounceable. ;)
If I may make a supposition, this occurs most often because programmers think they know how to program in Javascript, but don't have the first clue. That's why we see lots of code like: Or: Anyone want to guess what happens when "equation" is equal to "x ^^-Much more portable as it can be constrained to a single JS file without overriding the ability of other code to receive events. ^^-Seems more complex, but does not suffer from special character issues -AND- can be far less code when used in well-written algorithms.
Then there are people who do this: I hope you don't have a loop in doSomething, because you may accidently modify the global variable "i". The correct solution is to make it a local variable like this: Lastly, people complain that Javascript isn't Object Oriented. To which I can only act annoyed. What is this? If that still doesn't convince you to take another look at Javascript, well maybe this will:
http://www.youtube.com/profile?user=thewiirocks
It's a set of videos showing Javascript games in development for the Nintendo Wii. Nothing special here, just Javascript + Canvas. The Tetris game even has a demo here:
http://java.dnsalias.com/tetris/
Make sure you have a browser that doesn't suck.
I hope you all consider taking another look at Javascript. Maybe this book is for you, maybe it isn't. But there is a LOT of potential to release.
Javascript + Nintendo DSi = DSiCade
I used to hate javascript, not the language but the incompatible MSIE and Netscape bindings. As a language ECMAScript is so small and simple, I'm not sure it justifies a full book unless it's teaching basic programming.
.NET to desktop widgets, flash and the Mozilla platform. I'm not saying it's a great scripting language but if I had to choose just one scripting language based on availability it'd be javascript.
The thing about javascript is that it's the de-facto scripting language, from serverside development with Java, and
Ironically for 10 years I've disabled javascript for web browsing and I'm not enabling it any time soon (Flame away)!
I have no reason to be annoyed with "Javascript is not object-oriented", because it really is such an obscure bit of information. You're actually the first place I've ever seen to even mention it. Sure, I've noticed it used in some places, to which I thought "Javascript is object oriented? I didn't know that.", but nobody seems keen to tell anyone else ;)
-- 'The' Lord and Master Bitman On High, Master Of All
Should I embed JavaScript into HTML by writing or should I bow to the w3 consortium and write ?
I no how to program in php and C (enough to do what I want). I just cant wrap my head around java.
Libertarian Leaning Political Discussion Forum.
perdonad el oftopic... pero... como podría suscribirme a una lista de distribución en la que me enviaran emails de artículos de mi interés? Por ejemplo cine, informática, cocina... No digo que me paguen por ello, pero si que me envien cosas interesantes. gracias y perdonad el oftopic! mi mail es consejoinf@ctima.uma.es espero vuestras respuestas.
The frightening part is that it's not that obscure. The Client-Side Guide that Netscape used to publish (up until the ECMA standard was established), has several detailed sections on written OOP code in Javascript. Including multiple methods for handling inheritence! The problem is that no one read Netscape's documentation. Everyone "learned" Javascript by modifying some poorly-written script they found on the Internet, just so they could make text scroll across the status bar of the web browser.
And then developers wonder why they never knew XYZ about Javascript.
Javascript + Nintendo DSi = DSiCade
no != know
Java != Javascript
I know I shouldn't, but it looks so hungry...
Multiple methods? You can build up objects in C or Perl, but they are certainly not object-oriented languages. A basic feature of an object-oriented language is that it provides standard semantics for all objects written in that language.
An object-oriented design can be implemented on any platform.
You mean one of the hacks for using class/methods or are you just talking about creating standard objects? Since all objects extend Object(), can I just extend that or one of the other builtins?
I haven't RTFBook but any good web-development book must show how to code for graceful degradation. I hope this book does this.
Many web browsers do not support all features of JavaScript, and many that do allow users to disable certain features.
Handling these situations gracefully is essential to any good web site. Features such as JavaScript should be used to enhance a web site, not provide basic functionality.
Knowledge is how to play a game, intelligence is how to win, wisdom is knowing what game to play.
Indeed it can. However, Javascript was built with OOP in mind. Its multiple paths through everything is a nod to the fact that it's still a scripting language. i.e. Designed for a blend of brevity, flexibility, and fast interpretation. That's why Javascript has about three different methods to do everything.
Because sometimes: is preferrable over: It all depends on what you're trying to accomplish. Javascript gives you the most flexibility in exchange for somewhat "soft" semantics. Such is life.
Javascript + Nintendo DSi = DSiCade
Object oriented Javascript is used everywhere, if you care to look. It's the easiest abstraction which allows a script to be used in more than one place on the same page if it has to keep some state. It also avoids name clashes between global variables, which is important because Javascript programmers tend to use short names to save bandwidth. Clearly defined interfaces allow for compression of all internal variable names without harming external compatibility. If you're using Javascript for more than event handlers which hide and show pieces of the DOM, you should consider using OOP for your own scripts.
is this. Seriously.
Used lots of places (especially recently). I specifically said that in my first post: I did care to look, and low-and-behold it had objects.
But look for a javascript tutorial. You'd not guess that it allows user-defined objects from the first five hits, and you won't bother to look further since the first five hits seem so in-depth.
I've never read the official C standard, but because I use it every day, I assume I know C.
I hear the official javascript standard is poorly-written crap. I've never read that either.
Standards are for idealists. Other people's code is for programmers. When I used javascript, I read other people's code until I had the basics down, then bought a book and read that. I haven't used it in years (for anything non-trivial), but recently needed to do something, so I read other people's code. Hey look, objects! The book didn't mention those..
but then I use perl, so what's a "standard"?
-- 'The' Lord and Master Bitman On High, Master Of All
Gosh darnit AKAImBatman, stop spreading the secret of OO-JS everywhere!!! How are we supposed to look like genius web programming wizards if everyone knows how our tricks work?
;-)
;-) j/k
Heh, yep. I've been interviewing potential web application developers for months, and one of the things I try to probe is how much they really know about Javascript. So far I've met exactly zero who knew more than <a href="javascript:void(0)" onclick="alert('kewl')">.
...following the principles of Heisenburger's Uncertain Cat...
Working examples - isn't that what the Internet is for? If I see something neat going on on someone's site, the first thing I generally do is to borrow the code. Also, why advertise this on SlashDot? If you didn't learn Javascript in about half a day 10 years ago, you're probably not welcome here anyway.
Yeah.. JavaScript is so much more powerful that people think. I went about designing a javascript ASP.NET DatePicker I ended up in the world of Object Orients JS... its a little different but with concepts like prototypes its amazing to what extent you build interfaces that can do almost anything the desktop can do. Dainty Date is open source and can be downloaded here http://www.customcontrolfreak.com/
My favorite use for the squiggly brace object declaration is to use it to pass a variable number of parameters to a function. Just define the function to take one object, and load that object up with whatever data is relevant. Easy way to future proof functions when you know you'll probably be adding more parameters in the future.
Unfortunately, I found out about Douglas Crockford's lectures only *after* reading the spec :-\
You can pass any Javascript function any number of arguments. If you pass less than the defined parameters, the surplus variables are "undefined". If you pass more than the defined parameters, you can get the values from the parameters[] array, which stores all parameters.
Ah, yes JSON. I like that. Also cool is creating array objects using []; This is the foundation of many AJAX programs I have written. You write a service that passes something like:
[
['chris','jones','7182232000','programmer'],
['miles','davis','7182232000','musician'],
['hobart','macarthur','7182232000','lawyer'],
['bobo','clown','7182232000','clown'],
['chew','bacca','7182232000','wookie']
]
Leaner than XML, and you have a wonderful 2D array. Just return it to the page via an http request, and you have easy, lean, and fast AJAX code. Evaluate the object, loop over it, build HTML elements, et viola! instamatic content. Good stuff. And they say Javascript isn't powerful. Arrays, functions, they are all objects. Just have to tap into the power of the language.
blah blah blah
I wouldn't go down the prototype/scriptaculous route. Those libraries are powerful, but so is jQuery, and it's much more lightweight and easier to learn. Fun, even.
If you want to understand unobtrusive javascripting (which is the only way to go really, unless you don't mind your site's content being invisible to search engines), PPK's book is the best I've seen.