You Don't Know JS: this & Object Prototypes

Author: Kyle Simpson
Publisher: O'Reilly
Pages: 174
ISBN: 978-1491904152
Print: 1491904151
Kindle: B00LPUIB9G
Audience: Intermediate level JavaScript programmers
Rating: 4
Reviewer: Mike James 

A small focused book on the two most difficult topics in JavaScript sounds like a really good idea.

This is the second book in the You Don't Know JS collection. but in many ways it probably should have been the first. The reason is that the majority of programmers misunderstand the use of this and the so-called prototypical inheritance that JavaScript offers. Then there is the second big problem of programmers coming from a class-based, object-oriented background, and that's most of us, trying to turn JavaScript into something it isn't - again this mostly centers on clever use of this and prototype. 

 

Banner

 

I have a lot of sympathy for the attitude expressed by this book. JavaScript is different from other languages and to bring your object-oriented preconceptions to it makes it more difficult to use to create good programs. The author argues this point well, but be prepared for parts of the book telling you what class-based, object-oriented programming is just so that it can be explained how different JavaScript is. You will probably find a lot of this as revision, although there are some places where you will probably find the explanations a bit strange. For example, there really isn't an adequate explanation of the class-based type hierarchy and the explanation of polymorphism isn't one I recognize. 

There is another important problem. The book is short, but even so, to make the material spread, the information is slowly dripped out to the reader. You have long sections where what JavaScript isn't is explained in minute detail. On many occasions I wanted to shout out "so what is it then?!" When the point is reached, the explanation is often over long convoluted and fails to capture the underlying simplicity. 

For example, the way this works is very simple and there are only two general cases. Either it is being used within a function called without the use of the new operator when it is the call context, i.e. the object that called the function; or it is used in a function with the use of the new operator when it is a reference to the new object being constructed.

Once you have these two ideas in your head you can start to explore how they affect the meaning of the code you write. The book starts out by exploring the meaning of the code and makes a list of different conditions that this takes on the two meanings without really ever summing it up in a nutshell. The result is you are likely to understand the use of this better after reading the book but you might just end up confused. 

  

This tendency to over-complicate runs throughout the book. Chapter 1 starts with some things that this isn't but introduces the call method rather than staying simple with an object-based call. It then goes into a side issue of how a function can refer to itself and this is not by using this - at the end of this chapter you stand to be more confused by this. 

Chapter 2 attempts to sort it out by explaining that this is the call context. Here we meet the confusion caused by using strict, i.e.with no default to the global object for this if you call a function directly.

The summary of how this gets a value would be better done by explaining that it is the call context if you don't use new and it is the constructed object if you do. Then you can explain that the call method lets you set the call context and strict messes up the use of the global object. 

After covering this in detail the book goes off at a slight tangent to consider Objects - getters, setters, properties and so on. Chapter 4 goes over class-based object-oriented programming and uses the example of an inheritance hierarchy vehicle -> car and so on.

It gets a bit odd when polymorphism is defined a being able to distinguish, and so call, the method belonging to the super class in an overriding method. In my understanding polymorphism is deeply connected to the idea of type, i.e. which method you get depends either on the runtime type of a variable, i.e. late binding/virtual method, or the compile type declared type of a variable, i.e. early binding/non-virtual method. As JavaScript doesn't have a type hierarchy there isn't much to say about polymorphism.

Chapter 5 focuses on prototype and this again seems overly complicated. The basic mechanism is well explained but then we have a long explanation of when a property will shadow the prototype object and a long discussion of why the default prototype object includes a constructor property. The argument is that this property is irrelevant as the constructor is just some function called with new in front of it. This isn't quite true as the constructor is JavaScript's substitute for type. Any object created by a constructor could be thought of as of the same genearal type - after all they initally have all the same properties and the same prototype chain. I agree with the book that this isn't a particularly useful idea and so I'm in sympathy with the view that we could ignore it, but not for the reasons given. 

The book closes with a look at how to use class and no class "inheritance" and a look at ES6 which is constantly referred to as being beyond the scope of the book, but coming soon in a next book in the series. 

thisbook

This is a difficult book to come to a conclusion about. It is a book that takes the right approach, i.e. treat JavaScript as JavaScript and don't try to make it something else, but often I found the logical organization of what JavaScript offers overly complex. It is so much easier to give the grand organizing principle and then list the exceptions than to list every possible case as if the sum total was the grand principle.

If you do know JavaScript then reading it will be fun because you can spend time working out why you don't think about it quite in the same way. If you don't know JavaScript at this level then although you will get a lot out of this book, you run the risk of being confused because it could be so much simpler.  

 

Related Articles

Just JavaScript - In The Beginning Was The Object 

 

Banner


Modern Frontend Development with Node.js

Author: Florian Rappl
Publisher: Packt Publishing
Date: November 2022
Pages: 208
ISBN: 978-1804618295
Print: 1804618292
Kindle: B0B9BH5WBS
Audience: Node.js developers
Level: Introductory/Intermediate
Rating: 3
Reviewer: Ian Elliot
Modern development - what else is there?



Python All-in-One, 2nd Ed (For Dummies)

Authors: John Shovic and Alan Simpson
Publisher: For Dummies
Date: April 2021
Pages: 720
ISBN: 978-1119787600
Print: 1119787602
Kindle: B091DGDLK8
Audience: People wanting to learn Python
Rating: 2
Reviewer: Mike James
All-in-one refers to the fact that this is seven books put together - why?


More Reviews

Last Updated ( Wednesday, 14 June 2017 )