Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is the book titled "Object-oriented JavaScript"?  RSS feed

Stoyan Stefanov
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems like this is becoming an FAQ so let me try my best

People asking the question come from the two very different points of view and background. First, there are front-end developers saying that in javascript almost everything is an object, so this title is a repetition, like "green green olives". There's no "normal" javascript, only the OO kind. This is absolutely true, although if you try hard you can pretend OO doesn't exist. The other point of view is "what? you kidding me? JS ain't no object-oriented, it's a joke". Believe me, when I was telling people I'm writing a book and they go "what's the title", and I go "OOJS", I heard several times "javascript can be object-oriented? wow, I had no idea." So I guess my aim was to address the second point of view. Sometimes buzz-y kinda words help a concept sink in, like "enterprise", "patterns" and yes, "object-oriented".

In JavaScript there are five primitive data types: number, string, boolean, undefined and null. Everything else is an object, including arrays and functions, even null is an object. In addition there are wrapper objects around the first three primitive data types - Number, String, Boolean. It's also easy for primitives to turn into objects. Take string, for example:

Primitives turn into objects if needed, behind the scenes. A primitive is not an object, so it doesn't have properties, but you can still do:

Now functions. Functions in JS are first-class objects. You can do:

or also

Your function has properties and methods:

You can pass it around:

( Hmm, post getting longer, author not getting to the point )

Anyway, the point is that JavaScript is very very object-oriented, we can call it object-based if we want, to differentiate from "classical", or class-based languages, such as Java, we can also call it "prototypal". In any event, it's a very expressive, dynamic and fun language.

The book is also called OOJS to highlight the fact that it focuses on the core language, not so much on DOM scripting, browser and Ajax. There's a chapter devoted to these topics but it's more of an illustration what type of objects and interactions to expect in the real world, and also to provide some practical background for the next chapter "patterns" as to why these patterns and how do they help.

The book is also OOJS, because it's focus in on OO concepts and patterns. In JS there are many ways to do inheritance, depending on what you want to do and your personal style, so this is looked into in details. Also achieving things for which JS doesn't have specific syntax, such as private properties and namespaces (packages). Special attention is paid to JS-specific topics such as the lexical scope, prototype and closures.

Yet another reason is that the book is also aimed at developers who may or may not have some copy-tweak-paste type of javascript experience and no OO background. For them, this is a chance to deep dive into (and maybe relearn) the language and also familiarize with the OO concepts in general.

I hope the book helps establish JavaScript as an "enterprise" kind of thing, whatever meaning people put into this term. The aim is to position the language as something that deserves respect and attention and so do the front-end developers that use it.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!