Prototyping

Prototypes in the Screeps API can be modified by players. This allows custom methods to be defined directly on game classes such as `Creep` or `Room`. Unlike changes to individual game objects, changes to prototypes are retained across ticks, and they apply to all objects of that type instantiated after the prototype is modified.

Contents

This technique allows a developer to define features that can accessed directly from a game object. For example, one could define a custom travelTo method on Creep as an alternative to moveTo. It is even possible to overwrite/redefine existing properties/methods (known as "monkey patching"), though this can have unintended side-effects.

For more details and examples, check out the wonderfully-written official documentation.

Caveats

Static class methods and properties (ex: all of the fields on Game) cannot be modified via prototypes.

Some game types (ex: RoomPosition) are not interned. As such, if a memoized property is defined on such a class's prototype and later invoked on an instance of that class, the memoized result won't necessarily be available from other instances representing the same room position.