Apr 29, 2015

Notes on object-oriented JavaScript for o-o developers (part 1)

I've started reading The Principles of Object-Oriented JavaScript by Nicholas C. Zakas.

So far, it's been excellent, one of the best technical books I've read. It's clear, concise, and doesn't have any fluff, a solid tutorial with fewer than one hundred pages.

Coming from having programmed a lot in the strongly-typed language C++ a couple of lifetimes ago, I find JavaScript very fluid and sometimes very different in certain aspects.

It has some commonalities with PHP, which is what I'm mostly working in these days and still learning.

The purpose of this post and its follow-up is to highlight those JavaScript features  described in this book that might be of particular interest to someone with a background similar to mine.



**  Javascript does not have classes nor class definitions in the usual sense.

It does have objects, which are instances of reference types. The built-in reference types include Object, Array, Date, Function, RegExp, and Error.


**  An object is a dynamic collection of properties and their values.

Properties can be added or removed from an object at any time. In other words, by default, an object is not constrained by whatever type was used to instantiate it.

For example, create a Date object, then add an arbitrary property to it. Note that assigning a value to a property that didn't exist before implicitly adds that property to the object.

  var date = new Date;
  date.blessing = 'May you live in interesting times';

Then delete the property. The date object is now back to its initial state.

  delete date.blessing;


**  The primitive types, which include string, number, and boolean,  sometimes can be treated as objects even though they are not objects.

Each of these primitive types has a corresponding primitive wrapper type, respectively, String, Number, and Boolean.

The JavaScript  interpreter creates temporary objects of these primitive wrapper types as needed, uses them, and then discards them immediately. This is how properties and methods can seemingly be invoked on primitive types.

For example, extract the first character of a string using a method call.

  var first = 'hello'.charAt(0);


**  Functions are first-class objects.

That is, functions are objects. So functions are values that can be assigned to variables, passed as parameters, be returned, and appear in expressions, just like values of other types.


**  There are two literal ways of defining functions.

The first is the familiar declaration that starts with the keyword function and must include a function name. For example,

  function helloWorld() {
    console.log("hello, world");
  }

The second way is an expression that also starts with the keyword function but does not include a name. For example, this code defines an anonymous function and assigns it to a variable.

  var helloWorld = function() {
    console.log("hello, world");
  }


**  A function declaration may appear after a call to that function.

For example, this is allowed.

  helloWorld();

  function helloWorld() {
    console.log("hello, world");
  }

The function declaration is effectively hoisted to the top of its context.

But this only works for function declarations, not for function expressions. The code below results in a runtime error.

  hello();

  var hello = function() {
    console.log("hello");
  }

(Continued at Part 2.)

No comments:

Post a Comment