The Book of JavaScript 83
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.
Subtitle of the book (Score:2)
Re:Subtitle of the book (Score:5, Informative)
Of course, you'll need a decent library like MooTools or Prototype or such if you want to keep your programming sane, and especially if you want to do good stuff in a web browser (they abstract away an enormous swath of the IE vs Mozilla-type vs Opera/etc compatibility issues).
Re: (Score:2)
Re: (Score:3, Informative)
That's pretty simple actually. I used to have my own library that did that and more. But Proto
Re: (Score:2)
Yeah, my favorite one right now is how datevar.getYear() returns 107 in Firefox and 07 in IE. Just use datevar.getFullYear(), I know. But still...if you didn't know that imagine how frustrating it could be to have some date code return different stuff in different browsers?
Just like you said, aside from stuff like that, javascript is very powerful. Everyone has a somewhat new browser, so everyone can use it.
Re: (Score:1)
Re: (Score:2)
The fact that functions are objects is cool but I have many complaints about JavaScript. The implementation of object orientation as a hash table with a dot notation is a far cry from object orientation.
I have never used a java script environment outside of a web browser so I pretty much associate the two as the same. The fact that the this keyword has very different semantics depending on how the member function was called is a major stumbling block for me. Yes, prototype binding saves the day but it r
Re: (Score:2)
What difference does that make to you as a programmer, practically? Any dynamically interpreted language already uses a hash of some sort for runtime resolution of values in symbol table. Is this somehow less valid because it isn't precomputed? I guess you lose some of your type-safety-ness and are allowed
Re: (Score:2)
So what object-orienting features exactly are you looking for that JavaScript doesn't provide?
Proper overriding of members such that the base class implementation is still available (e.g. super.mymethod()).
The one thing I could see you complaining about is data hiding and private variables - okay, those are a little trickier and involve setting an object's functions that use these private variables in the constructor at runtime.
Any well defined task can be programmed in any general purpose programing language. So whether or not something is possible is not a good measure of a programming language. By that criteria, all general purpose programming languages are identical. Rather, how easy it is to do something is a better measure of a programming language. Yes, it is possible to fake all kinds of object oriented f
Re: (Score:2)
I got into this originally because I spend a lot of time working on SeaMonkey (and sometimes Firefox / Thunderbird), which has huge amount
Re: (Score:2)
Re: (Score:1, Funny)
while(1==1){
alert('pwned');
}
</script>
Mwhahahahah
Phear my mad skillz!
Hunrf?!?!
Re: (Score:1)
Me: "Yes<click>, please"
Browser: "Would you like me to smack balbord while I'm at it?"
Me: "Yes<click>, please"
(Okay, so that last part doesn't really happen. But there's always hope in the next version of Firefox!)
cmon (Score:2)
Re: (Score:2)
Re: (Score:2)
Re: (Score:2)
haha, not to us poles!
silly americans...
Time to Learn How to Program (Score:5, Informative)
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 [youtube.com]
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/ [dnsalias.com]
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.
Re: (Score:2)
is supposed to say this:
Just more proof of why coding for the DOM is a good idea. Microsoft's extension crud may seem convenient at the moment, but it's just hurting you in the long run.
Re: (Score:2)
However, most of your examples don't have squat to do with the core language, and everything to do with the DOM. Or what *you* think is a useful way to express an idea.
Just because it's possible to manipulate the DOM to insert an event listener doesn't mean that it's practical. Especially in the case when your attaching say events to HTML elements that run java
Re:Javscript Object Oriented? (Score:2)
Re: (Score:2)
The frightening part is that it's not that obscure. The Client-Side Guide [sun.com] 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 In
Re: (Score:2)
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 p
Re: (Score:2)
Re: (Score:2)
[
['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 requ
Re: (Score:1)
;-) j/k
Re: (Score:2)
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')">.
Re: (Score:2)
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 ei
Re: (Score:2)
Unfortunately, I found out about Douglas Crockford's lectures [yahoo.com] only *after* reading the spec :-\
Re: (Score:1)
That's a first-class function or closure, not a class. You still can't do class or private and protected methods using this hack, nor can you do any meaningful inheritance or mixins.
You're right, Javascript is a good language with an unfortunate reputation, but it ain't object oriented. Its functional, and thus frameworks are built with first-class function and closures.
Re: (Score:2)
You don't need a class/method design to be object oriented. Only the OO purists are dead-set on that. OOP is a style of programming, with some languages providing first-class support while other's do not. Nearly every language can be coded in an OO-manner to some degree. Javascript, thankfully, got fairly decent support from the get-go despite its intended use as procedural scripting. That makes a big di
Re: (Score:3, Interesting)
Re: (Score:2)
Thanks for the link, that's interesting.
Re: (Score:2)
Scope hacks are not a way to achieve member visibility, it's just that: a hack.
It adds overhead to the constructor, wastes memory by keeping the construction activation object alive long after the constructor function has returned, doesn't let the public methods access the "private" ones (ooops...), doesn't resolve the issue of protected members.
Well, for protected members maybe it doesn't matter since extending "classes" in JS is he
Re: (Score:1)
Do you consider Python to be object-oriented? The "private" members of classes on Python are defined such that the names are prefixed with 2 underscores, i.e. __foo is private while bar isn't. The private var names are mangled so that they aren't easily accessible outside the class, but they are still technically public.
You can use this same idiom with JavaScript - just prefix the members you intend to be private with 2 underscores (or something equivalent). There's no name mangling in J
Re: (Score:1)
Well, sure, I'm with you on this one, but you have to admit that this is how most of those programmers are "teached" from javascript documentation sources (not even mentionning those "100 great javascripts" web sites). The diversity and absurdity of documentation surrounding JS and web development reached a dangerous level. I
Re: (Score:2)
Re: (Score:2)
That is exactly the case. Here's a bit of test code for you to try:
Re: (Score:2)
Both the DOM API and innerHTML have their place, and event handlers attached in the tag as attributes, and even eval() (yes OMGWTFBBQ!! I suck!!) can be useful for plenty of purposes, next to the "better" approaches you advertise.
You're simply repeating well known clic
Re: (Score:2)
1. It's not a default "cut and paste" rant. It's one I made just for this article. ;-)
2. That "Reply" button looked awefully inviting...
If truly you're wondering why I suddenly broke into a rant, it's because I've seen these articles too many times. Everyone assumes they already know Javascript, ergo, this book is not targeted at them. Well, someone needed to burst their bubble. Just loo
Re: (Score:2)
Indeed! So I just pasted it from your response, in case you forgot what you were talking about exactly 47 seconds ago!
BTW, Flash implements most of ECMAScript 4.0 in ActionScript 2.0. Which is Flash version 7.
If you can say "most" when Flash 7 implemented loosely the class syntax at compiler time, on top of their extended prototypes engine, added types only as a compiler time check a
Re: (Score:2)
function LightBulb(on)
{
this.on = on;
this.isOn = function() { return this.on; }
this.setOn = function(on) { this.on = on; }
}
var mybulb1 = new LightBulb(true);
var mybulb2 = new LightBulb
Re: (Score:2)
Ah well, at least someone's keeping me honest.
Re: (Score:2)
Re: (Score:1)
> they know how to program in Javascript, but don't have the first clue. That's
Amen. The situation out there is actually so bad, it boggles the mind how so many web developers continue to earn a paycheck. Want evidence? Turn on JS debugging/error messages in your browser (esp. IE) and spend a week or two battling the deluge of error dialogs before you turn it off again in disgust. If people can't be bothered to write ro
Re: (Score:1)
Typical.
If you know it so well, then please name at least two different methods of for creating your own objects in OOP-style Javascript. Ready? Go!
Re: (Score:2)
Re: (Score:2)
1.
2.
3.
Re: (Score:2)
hope it helps (Score:1, Offtopic)
Re: (Score:2)
Feeding Trolls (Score:2)
Java != Javascript
I know I shouldn't, but it looks so hungry...
Re: (Score:1)
Be aware of the least common denominator (Score:4, Insightful)
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.
Re: (Score:2)
But these days it's very hard to convince non-techies that web sites should often not require javascript for basic functionality. They see gmail or whatever and assume everyone on the net
Re: (Score:2)
If in doubt, TEST the functionality before calling said functionality.
Do NOT insert code switches based on browser sniffing. Never ever ever.
It really is just that simple.
The only book everyone needs (Score:1)
Lightweight and unobtrusive? (Score:1)
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 [quirksmode.org] is the best I've seen.
Still slashvertising books in 2007? (Score:2)
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
JavaScipt interfaces (Score:2, Interesting)