this & Object Prototypes

You Don’t Know JavaScript: this & Object Prototypes by Kyle Simpson
$19.62 at Amazon ($12.49 Kindle)

If there’s one thing that’s particularly frustrating in JavaScript, it’s this. this has an irritating tendency to suddenly refer to something entirely different than you were expecting, at which point everything breaks.

this is confusing because it’s runtime-bound based on the context of the function’s invocation: how it was called, where it was called from, etc. After reading this book, I’m still not always sure how to ensure that this refers to the correct context, but at least now I understand more about WHY it’s being irritating. Still, more space could have been devoted about why this works the way it does, rather than simply giving the list of rules that JavaScript follows to determine the context.

While this was the reason I got this book, it takes up only the first two chapters; the rest is devoted to objects. Objects in JavaScript can also behave in unexpected ways because JavaScript doesn’t really have classes, and inheritance doesn’t work as you would expect it to in an object-oriented language.

I found myself rereading material in this book as I worked through it, but I think that’s a function of the language rather than the writing; JavaScript has just never clicked for me. I deducted a star from my rating because the book didn’t really tell me what I wanted to know about this, seeming to give a quick overview of the rules without much of the logic behind them and then jumping into objects, but otherwise I’d call it a pretty good book.

Disclosure: I received a free review copy of this book.



This entry was posted by William on Thursday, November 6th, 2014 at 2:49 pm and is filed under Technical . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Comment

  1. Bradley Meck says:

    Rereading : might help, just remember you need to create a function wrapper that sets up *how* your function will be called (regardless of how the wrapper is called).

Leave a Reply