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:
- CommonJS modules.
- Composition instead of inheritance.
- Object linking/delegation.
Additional benefits
- No need to invoke
newso there is no possiblity of subtle construction bugs. - Mitigates the need to wait for special
classorconstructorsyntax 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 methodboilerplate. - Removes the temptation to architect classes when functions will suffice.
Objects to focus on:
Running the unit and integration tests
npm install
npm testCompatability
- 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.assignso you'll need a shim or an alternative if you intend to use in older JS engines. - Uses ES5
Object.createso you'll need a shim or an alternative if you intend to use in older JS engines.