GitHub

OLOO (objects linked to other objects) pattern explored through game player objects.

This POC is based on a comment on an article called JS Objects: De"construct"ion. This POC was not about improving the original RPG's game mechanics so I didn't...it's about providing a clean example of linking objects in JS.

Overview

JavaScript has a very elegant behavior delegation system via object linking; however, instead of embracing this simplicity, many JS developers blindly subscribe to the notion that JS object construction must look similar to Ruby, Java, C++, etc. This is not only untrue, but it is an unnecessary abuse of the language.

This POC builds upon three principles:

  1. CommonJS modules.
  2. Composition instead of inheritance.
  3. Object linking/delegation.

Additional benefits

  • No need to invoke new so there is no possiblity of subtle construction bugs.
  • Mitigates the need to wait for special class or constructor syntax introduced in ES6 to be avaialble and no need to transpile to get it.
  • Mitigates the need to have a ton of indentation up front so code doesn't start out looking messy.
  • Mitigates the need to write Ctor.prototype.method = function method boilerplate.
  • Removes the temptation to architect classes when functions will suffice.

Objects to focus on:

Running the unit and integration tests

npm install
npm test

Compatability

  • Uses ES6 object literal shorthand assignments for brevity so you'll need a capable JS engine or you'll need to transpile.
  • Uses ES6 Object.assign so you'll need a shim or an alternative if you intend to use in older JS engines.
  • Uses ES5 Object.create so you'll need a shim or an alternative if you intend to use in older JS engines.

Resources

Read the original on github.com ↗