Most of Walker Christian Forrester's code on GitHub is full apps: a farmers market finder, online stores, his first mobile app. One of the most interesting pieces is much smaller. Greetr is a JavaScript library of greeting functions. In the words of his README: "When given a first name, last name, and optional language, it generates formal and informal greetings."
You can try it at walkforr.github.io/greeterLibrary: pick a language, type a first and last name, click Login, and the page greets you.
A classic exercise, taken further
Building Greetr is a well-known exercise from Anthony Alicea's course JavaScript: Understanding the Weird Parts, in which students write a small library the way jQuery is written. Walker's version goes well past the basics. Its list of supported languages has 21 entries: English, Spanish, Mandarin, French, German, Italian, Portuguese, Hindi, Persian, Russian, Japanese, Korean, Turkish, Mongolian, Kazakh, Hungarian, Arabic, Hausa, Swahili, Dutch and Indonesian. Each language has two greetings, an informal one ("Hola") and a formal one ("Saludos").
Using it
Here is how the demo page uses it, from the script behind the demo:
let name = G$(firstname, lastname);
name.setLang(lang).jQueryGreet('#greeting', true).log();
That one line sets the language, writes a formal greeting into the page and logs who signed in. Four ideas make it work.
1. Private data in a closure
The whole library is wrapped in a function that runs as soon as the file loads, a pattern called an IIFE (immediately invoked function expression). The language list and the greetings live inside it. As Walker's comment puts it, they are "hidden within the scope of the IIFE and never directly accessible." The library's own methods can read them, but code outside can't change them. That's a closure, and it's how a library keeps its internals safe from the page around it.
2. Shared methods on the prototype
The methods (fullName, greeting, formalGreeting, greet, log, setLang and the rest) aren't copied into every greeting object. They sit once on the prototype, which every object shares. His comment: "prototype holds methods (to save memory space)". Make a hundred greeters and there's still only one copy of each method.
3. No new keyword
Normally you'd write new Greetr(...) to make an object. Greetr hides that. Calling G$() creates the object internally, using an init function whose prototype points back at Greetr's. The comment explains why: "allowing us to 'new' an object w/out calling 'new'." jQuery does the same, which is why you write $('#id') and never new $('#id').
4. Chaining
Every method that changes something ends with return this, which hands back the same object, so the next call can follow right after with a dot. That's why setLang(...).jQueryGreet(...).log() works as one line. His comment on it is simple: "makes the method chainable".
Safety and choice
Two more details show some care. setLang checks the language against the supported list, and if it isn't there the library stops with an "Invalid language" error instead of printing "undefined Walker!". And Greetr can write into the page two ways: jQueryGreet for pages that use jQuery, and vanillaGreet, which uses the browser's built-in document.querySelector and needs no jQuery at all. The README promises both: "Supports jQuery and vanilla js query selectors."
Why a small library is worth reading
Greetr is about 200 lines of code, but it covers ideas that confuse a lot of new programmers: closures, prototypes, the this keyword and method chaining. Writing it yourself is a good way to learn how the libraries you use every day are put together. It fits next to the projects from his Georgia Tech coding boot camp, and the code is on GitHub for anyone who wants to read it.