Posted by Walker's family on his behalf.

Some code is worth reading because it is short. Greetr, a JavaScript library by Walker Christian Forrester, fit in about 200 lines, and his family pulled out the parts a newer developer might want to study.

The job it had

Take a person's first name, last name and, optionally, a language. Return a casual greeting or a formal one. The supported list held 21 languages, including Mandarin, Hindi, Persian, Mongolian and Hungarian. A demo at walkforr.github.io/greeterLibrary let anyone try it in the browser.

Where it started

Anthony Alicea's course, JavaScript: Understanding the Weird Parts, includes a well-known task: build a small library in the style of jQuery. Walker used it as a base and kept going.

Reading order for the code

  • The wrapper. The file opened with an immediately invoked function expression. The language data stayed inside its scope, so outside scripts had no route to change it.
  • The prototype. Greeting, logging and language methods were defined once and shared by every object, which kept memory use down.
  • The constructor trick. An internal init function pointed its prototype back at Greetr's, so G$() handed over a ready object and callers skipped new.
  • The return value. Methods that changed something ended in return this. That is what made calls such as setLang() and log() chainable.

Two practical details

  • An unsupported language raised an "Invalid language" error rather than printing a broken greeting.
  • Page output came in two flavors: jQueryGreet for jQuery pages and vanillaGreet, built on document.querySelector, for pages without it.

Who might enjoy it

Anyone still getting comfortable with closures, prototypes or the this keyword could trace all of them in a single file, which Walker published on GitHub.

His family's longer walkthrough, with the example code, is in the Greetr article on walkerforrester.com.