sabell2012
Mega Sage
Mega Sage

NOTE: MY POSTINGS REFLECT MY OWN VIEWS AND DO NOT NECESSARILY REPRESENT THE VIEWS OF MY EMPLOYER, ACCENTURE.

 

DIFFICULTY LEVEL:  INTERMEDIATE to ADVANCED
Assumes at least an intermediate knowledge of the JavaScript programming language. I highly recommend checking out the learning links here if you would like to improve your knowledge of JavaScript.

Community Code Snippets - Suggested Scripting Resources 

 

NOTE: I consider this to be a Intermediate to Advanced book as it contains material that even expert level developers will use in their day-to-day work.


My thought was to publish out any book reviews on JavaScript or relevant topics that I had recently read. The idea was to do this from the perspective of how it would benefit a ServiceNow developer. With that in mind here is my first one!

 

Principles of Object Oriented JavaScript

 

by Nicholas C. Zakas

Publication Date: February 2014, 120pp.

ISBN: 978-1-59327-540-2

https://www.nostarch.com/oojs

(This is a review of the Kindle version of the book)

 
sabell2012_1-1700532371072.png

 

Awhile ago I was browsing through Amazon looking for good JavaScript books to read. Specifically I was looking for books that addressed advanced topics. This one popped out of the mass. Having come from several years of Object-Oriented Programming (OOP) with C# and .NET I was intrigued with the title.

 

According to the father of OO everything, Grady Booch, an OO language should cover the following at a minimum:

 

  • Encapsulation
  • Aggregation
  • Inheritance (Hierarchy)
  • Polymorphism

 

As you are probably aware JavaScript claims to be an OO language, but of the four listed tenants I have always considered JavaScript a wannabe or on a scale of 4 (with 1 being the lowest) a 1.5. For example there are no object classes in JavaScript; so how does that work?

 

How does this stack up in the book? Zakas does a really great job of addressing all of the four tenants. Kind of took me aback. Especially the Encapsulation bit, and Polymorphism; I had not considered his approach.

 

So, what is in the book?  

 

Chapter 1: Primitives - In the first chapter Zakas works through primitives and reference types. He does a nice job describing what occurs under-the-hood with JavaScript, and how we need to be thinking when doing object development.   For ServiceNow developers (both beginner and advanced) it is a good review.

 

Chapter 2: Functions - a nice break-down of the different function types in JavaScript, and how they can be naturally oriented to do object development. How functions are created, accessed, and destroyed. Then surprisingly, a section on how to do overloading! Again, still mulling over his approach, but I am liking it. He does a good, but brief job on the "this" object. This section is great for general ServiceNow script development with a lot of explanation on why JavaScript works the way it does.

 

Chapter 3: Understanding Objects - some expansion on topics covered by Douglas Crockford in his book Javascript: The Good Parts, and some simple, but descriptive examples.   The section on detecting properties was the one I mentioned having the "light bulb turn on" moment on when I came up with my GlideRecord toObject method.   Another section that grabbed my attention was on Preventing Object Modification; almost impossible with JavaScript.   It was a good approach.   His sections on Sealing and Freezing Objects was important to me.   I will have to explore how to use all of this in ServiceNow, but the applications are there.

 

Chapter 4: Constructors and Prototypes - Zakas begins with describing constructors, their purpose, and their place in the tool box. Constructors are a little-used feature by most developers in the ServiceNow arena, and bear further study. Constructors are a major component of a true object oriented language. He gives a couple of very class-like functions which utilize a constructor, the "this" object, properties, and methods. The next section on Prototypes is the one that should be of great interest to the ServiceNow programmer. Here you will find a great description of how Script Includes actually function. Finally the author merges the two concepts, and gives a simple working example.

 

The final two chapters are what made me sit up as they are relevant to ServiceNow Scripting!

 

Chapter 5: Inheritance - with the idea of prototypes now under our belt the author describes how to achieve inheritance through prototype chaining. We see this done with the "extends from" function in ServiceNow Script Includes (SI). Here is a super description of how this actually works; how methods and properties are transmitted up the chain from each "parent" to the "child". With this description those of you who are familiar with classes will actually start seeing objects look like a class definition. Here we find out about how toString works (ever wondered?). Zakas also covers Constructor Inheritance. We see that with the GlideAjax SI in that we are told not to override the extended Ajax SI initialize method. This is a SI version of a constructor. The base Ajax SI already has this defined, and if you take a look it does some important stuff.   If we override it in our code it actually breaks the GlideAjax SI code!   An intriguing section near the end of the chapter is named Constructor Stealing. Here the author talks about a couple of little known (to ServiceNow developers) built-in functions called call() and apply(). This allows the coder to actually access an overridden method of an extended class. I will have to mess with this in conjunction with SIs. Wow, inheritance and aggregation!

 

Chapter 6: Common Object Patterns - finally, the chapter I had been waiting for. Zakas has taken us over the ground of Polymorphism, Aggregation, and Inheritance. How do we implement Encapsulation? Here the author dives into Private, and Priveleged members. The module pattern is covered, along with how do implement private members for Constructors. The author then tackles Mixins. Here is something I had not used in ServiceNow, and it left me to pondering how to implement it. So, look for an article on this in the near future. Lastly, considering how global JavaScript is, the section at the end on Scope-Safe Constructors was great.

 

Throughout each chapter the author builds off of simple JavaScript examples to explain why and how the code works the way it does.

 

I really enjoyed the book, it was short, but packed full; covering how to implement all of the tenants of an Object-Oriented language with JavaScript (or at least emulate them). Well worth the money spent, and I found several techniques that will either be useful in my day-to-day programming in ServiceNow, or explain already existing functionality where I was curious why something was implemented the way it was.

 

I guess this really shows what kind of nerd I am; taking the time to read technical books from cover-to-cover!

 

Enjoy!

Steven Bell.

 

If you find this article helps you, don't forget to log in bookmark it, and mark it as "Helpful"!

 

sabell2012_2-1700532959526.png


Originally published on: 10-04-2015 04:19 PM

I updated the code and brought the article into alignment with my new formatting standard.

2 Comments