Object-Oriented JavaScript book review

Tags:

I was recently contacted by Packt Publishing, and asked if I wanted to review a book they published – Object-Oriented JavaScript, written by Stoyan Stefanov. I hadn’t reviewed books before, but I decided to give it a shot!

As stated on Packt’s website, by reading the book you will learn:

Create scalable, reusable high-quality JavaScript applications and libraries

  • Learn to think in JavaScript, the language of the web browser
  • Object-oriented programming made accessible and understandable to web developers
  • Do it yourself: experiment with examples that can be used in your own scripts
  • Write better, more maintainable JavaScript code

It also says:

After reading this book, you’ll be prepared to ace your JavaScript job interview and even impress with some bits that the interviewer maybe didn’t know. You should read this book if you want to be able to take your JavaScript skills to a new level of sophistication.

which would definitely be a benefit, don’t you think so?

Does the book deliver all this?

The contents

The book is divided into 8 chapters, four of which deal with the basic features of the JavaScript language, and the other four dive deeper into the language and the actual object-orientation parts like inheritance and design patterns.

The book uses Firebug as the main “development environment”, which I think is a good thing. Firebug is an important tool for a JavaScript programmer, and it works quite well for the task of teaching and learning too.

The book uses tip boxes, which contain best practices, common mistakes or other useful information, and the code samples in the book are consistent in style and display use of good practices.

Each chapter also contains exercises in the end, which help you to learn the concepts introduced in the chapter.

The JavaScript basics

The first chapter begins by looking a bit into what JavaScript is: Where it came from, where it is now and also a bit about the future. It serves as a nice look into the language’s roots. It also introduces JavaScript and OOP concepts, and compares them with other languages.

Next, in the second chapter, the book looks more into JavaScript’s types, expressions and constructs. Everything is explained thoroughly, and using language that even beginner programmers should understand. It even explains terminology for words like “operator” and “operand”, so even if you’re not familiar with these terms – for example if english isn’t your native language – you won’t have any problems with the upcoming chapters. It also explains more obscure syntax like the exponential “2e-5” and special values NaN, Infinity and others.

The third chapter is all about JavaScript functions, again starting with basics, terminology and explaining things very thoroughly. It goes into more detail and more complex examples about functions too: anonymous functions, inner functions, etc.

As an added bonus, this chapter looks into some common JavaScript built in functions.

Chapter four introduces objects, which have been refered to a bit earlier in the book as well. As usual, it’s very clear and everything is explained well. It talks about how JavaScript’s object oriented features differ from other languages and provides some examples which are useful if you’re familiar with some “classical” OOP language. Like with the previous chapter, this one also shows you how to use built in JavaScript objects such as Number and Date.

More in-depth

The fifth chapter talks about the prototype property. You can read most of this chapter online here at my blog. There’s a bit more towards the end of the chapter which isn’t included in the post, though. This chapter is one of the most useful parts in the book for readers who are more experienced in JavaScript.

Chapter six is again a chapter looking deeper into JavaScript: inheritance. It explains various inheritance styles possible in JavaScript, such as prototype chaining and pseudo-classical inheritance. Also talks a bit about Yahoo! UI Framework and the Prototype library.

The seventh chapter is dedicated to the browser: The browser object model and the document object model. It covers cross browser compatible features of the BOM and DOM, like working with DOM nodes and using XMLHttpRequest. It also talks about the event model used in the browser, and some help on how to make things work in IE. It does not go into very great detail, but serves as a good primer to working with the browser’s features.

The final chapter looks into coding and design patterns. This one covers common things you see in modern JavaScript code, such as namespaces and initialization patterns. It also shows how to achieve public and private functions, and chaining, which is very common in jQuery. This chapter is very good, especially as many frameworks like Dojo and YUI use JavaScript in ways which can be confusing to beginners, but after reading this, you should understand them quite well.

Appendixes

The book also has four appendixes: A list of JavaScript reserved words, a list of builtin functions with examples, a list of objects with examples and a JavaScript regex syntax index.

Good things

The book is very thorough in explaining how JavaScript works. The first four chapters make the book suitable even for those who haven’t never used JavaScript before. They also contain some tidbits which more experienced programmers might not know about. The last four chapters make the book useful for people who are familiar with JS, and even those who are quite experienced in it can gain some new insights, for example about the prototype property or inheritance.

To highlight some good parts:

  • The first chapter shows a Firefox “strict” JavaScript parsing mode I did not know about
  • The third chapter explains “self executing function” syntax, (function(){ … })();, which used to confuse me a lot some years back
  • The chapters on prototypes, inheritance and patterns are excellent, and they had various small things I didn’t know about

Bad things

It’s difficult to find bad things about this book. Maybe the fact that I only have it as a PDF?

There were some small places for improvement, though. In the second chapter it might have been a good idea to mention that lazy evaluation in conditionals is also known as “short circuiting”.

Also, the third chapter code examples displaying inner and outer functions used f() and n() as the function names. It would probably have been a little better to use descriptive naming like outer() and inner(), so you wouldn’t have to look at the code example to see which one is which.

If you want to learn more about Ajax or DHTML-style tricks, this book may not be for you. While the browser environment chapter is good as an introduction, and you can definitely start using JavaScript on websites based on it, it doesn’t go into more advanced things.

The bottom line

To go back to the initial question, “does the book deliver?”, the answer is definitely yes. The book delivers all it promises, and after reading it, you will be able to impress interviewers.

All in all, the book is a great read for anyone who wants to learn JavaScript, or improve their existing skills in the language. The book is most useful for beginner to intermediate level JavaScripters, but may also contain some useful things if you’re more experienced.