Expert JavaScript

Author: Mark Daggett
Publisher: Apress
Pages: 248
ISBN: 978-1430260974
Audience: Intermediate JavaScript Programmers
Rating: 3
Reviewer: Ian Elliot

What does it mean to be an expert in JavaScript?

 

JavaScript is a very misunderstood language because people just pick it up and fail to understand its depth. When people do try to understand its depth then they usually end up explaining it by comparison with other languages. Sometimes these explanations are useful, in that they point out things you would otherwise miss about the language, but they are usually very incomplete and lack the structure which explains how the language works. 

This is a book written by someone who has spent a lot of time understanding the way JavaScript works and how you can avoid making mistakes. Unfortunately it is written from the perspective of other languages and it often fails to explain what is going on in the JavaScript - but this doesn't mean it isn't a useful book if you are keen to learn a bit more about JavaScript.

 

Banner

 

The book starts off with a look at JavaScript and its relationship to ECMAScript and to Java. It then considers what it is to be object oriented and promptly launches into a discussion of properties. This introduces the definePropery method and many other similar methods that don't work in all browsers. It is important to realize that while most of the features described in this book work on up-to-date browsers they won't work on legacy browsers such as IE8 or Firefox 3. There is also no discussion of how to cope with compatibility problems. Occasionally an ECMAScript 6 feature is marked as such, but again there is no discussion of what to do in older browsers. 

The first part of the chapter reads like a long list of methods that you can use to control properties and prototypes, but there is no real discussion of why these exist or what you should do with them. It then moves on to object creation - which probably should come first. After object literals, the new operator is discussed in terms of "this is not the new operator you are used to". This allows the idea of a constructor to be introduced and we then have the standard accounts of abstraction, encapsulation, polymorphism and inheritance - not all of which make much sense in a JavaScript context. The explanations also have a tendency to simply quote some powerful sounding phrase rather than really get to the bottom of what is going on. 

For example in the explanation of how to create a private variable, a trick most JavaScript programmers know, the explanation is that "because of the way JavaScript's Function level scoping works". This sounds good and it is sort of true but the real reason that the technique and many like it work is that you are creating a closure. It isn't so much that function level scoping stops you accessing the variables but given that the function no longer exists when the variables are accessed they should no longer exist. They only do exist because of the creation of a closure. This is never mentioned. 

Chapter 2 looks at functions and starts off by introducing statement blocks. Next we examine parameters including many features that only work under ECMAScript 6. The discussion of the differences between function declaration and function expressions does explain hoisting but in isolation - things other than functions are hoisted. The chapter closes with a look at higher order functions and the new ECMAScript 6 "arrow" functions. There is also another look at the difference between block and function scope - something that is only an obsession if you are comparing JavaScript to Java say. Block scope is useful but hardly essential and certainly not worthy of elevating into a principle that explains everything. 

 At the end of the chapter you haven't been told that functions are first class because they are object just like any other objects in JavaScript. Without this simple piece of information properly emphasized, things don't really make much sense. 

 

 

Chapter 3 deals with closures despite the fact that they have been used since the start of the book without a single mention. Again we have a look at the scope problem and the way ECMAScript 6 provides block context, but this time the main target is the this keyword. If the idea of an execution context had been explained earlier, or even at all, then this would not seems so mysterious.

It is a shame that the JavaScript execturion context is named "this" as it leads to confusion with this in class-based languages, but once you understand the execution context idea it isn't any more difficult. It also helps to stop confusing the problems that this causes with scope. The simple rule is that this is set to reference the execution context at the time a function is invoked and what that context is depends on how the function is invoked. It really doesn't have a lot to do with scope, i.e. what variables are accessible.

Finally the chapter ends with what you might use closures for. It mentions binding proxies and context for DOM manipulation. It doesn't mention the most common use for a closure - to create private variables - and it doesn't mention the standard pattern of using closures to provide event handlers and callbacks with additional data without changing their parameter signature. 

Chapter 4 is very strange. If you haven't seen the "WAT!" video, it probably won't make much sense. In essence it is a rebutal of the critique contained in the video. It first introduces the term "programatic jargon" which is itself programatic jargon because it replaces what most programmers would call "idiomatic code". We next have a long list of JavaScript behaviors and explanations of why they are reasonable. It would be much better to explain the general philosophy of JavaScript rather than the detail - then the whole WAT! video would be instantly recognizable as the ramblings of someone who doesn't understand JavaScript. 

Chapter 5 examines concurrency or rather asynchronous programming. This explains the way JavaScript handles events and eventually introduces the idea and problems of working with callbacks. The solution offered is to use Promises. It isn't an easy read and you might be left wondering how to implement Promises at the end of it. It then goes on to introduce generators and co-routines - a really nice way of working, but again no mention of the fact that these features aren't 100% standard across browsers as yet. The chapter ends with a look at WebWorkers. 

The rest of the book slowly drifts away from the core language to matters that are much more general. Chapter 6 is particularly strange in that it covers using JavaScript with embedded hardware, which is not something everyone thinks is a good idea. It probably should not be included in a book that really hasn't devoted enough space to its main topic. 

From here we return to more conventional topics. Chapter 7 is about style with a list of dos and don'ts. Chapter 8 covers workflow, build tools and unit testing. Chapter 9 is about code quality - metrics and so on. The final chapter is on testability and how to improve it. 

This is an advanced book on JavaScript, but it is arguable that it doesn't explain enough of the philosophy and workings of JavaScript to called an expert book. It doesn't even cover the majority of idioms you will find in books like JavaScript: The Good Parts or in well known code like jQuery.

If you read this book then you will know a little more about JavaScript at the end of it, but there will be many things you still won't know about and many principles that make JavaScript seem easy will still be hidden from you. The real practical problem with this book is that it makes no attempt to tell you how to write JavaScript that has a chance of running on a range of browsers.  

Related Articles

Just JavaScript - In The Beginning Was The Object

jQuery Promises, Deferred & WebWorkers

JavaScript Hoisting Explained

JavaScript Books (Round-up on Programmer's Bookshelf)

JavaScript: The Good Parts (review)

 

 

Banner


Wild West To Agile (Addison-Wesley)

Author: Jim Highsmith
Publisher: Addison-Wesley
Pages: 304
ISBN: 978-0137961009
Print: 0137961006
Kindle: B0BXWP88KP
Audience: Adherents of Agile methodology
Rating: 4.5
Reviewer: Alex Denham

The subtitle of this book is Adventures in Software Development Evolution and Revolution and it is personal reminin [ ... ]



Oracle PL/SQL By Example, 6th Ed

Author: Elena Rakhimov
Publisher: Oracle Press
Pages: 480
ISBN: 978-0138062835
Print: 0138062838
Audience: Developers interested in Oracle PL/SQL
Rating: 4
Reviewer: Kay Ewbank

This is the sixth edition of a well established title that has been updated for the latest version of PL/SQL (21c).


More Reviews

<ASIN:0596517742> 

Last Updated ( Wednesday, 23 April 2014 )