vigour-base
Extendable object constructors, build for speed, low memory consumption and simplicity
-
set method
-
easy extendable property defintions
-
deep, memory efficient prototypes
-
parent and key fields
-
types (easy inheritance)
-
inject pattern
Properties
The properties field is used to add property definitions for certain keys within set objects.
There are 4 types of property definitions:
true
clears any special base behaviour for the keyfunction
calls the function when the key is set instead of the normal behaviournull
removes property definition and any existing instancesanything else
uses the set function
basic
var base = properties: normal: true { thisspecial = val * 10 } base: nested: true base base
set
var special = type: 'special' var base = properties: // uses "noReference" for a base special: special base // add something to the "special" propertybase// → base.special.aField.val === true, inherits from the property
define
Allows for extra customisation of property definitions.
It has 3 options:
:key
when a property is set uses this key to store its value:reset
resets a previously defined property:val
property value
var base = properties: define: x: key: 'y' something: key: 'else' val: a: true b: true hello: key: 'bye' val: 100 x: 10 // → y: 10 something: c: true // → else: Base { a: true, b: true, c: true } hello: field: true // → bye: Base { val: 100, field: true } base
Context
Context enables deep memory efficient prototypes. Stores information on fields about first non-shared ancestors.
basic
Notice that base.a.b.c === instance.a.b.c
is true but the paths are different
const base = key: 'base' a: b: c: 'its c' const instance = key: 'instance'console // → trueconsole // → [ 'instance', 'a', 'b', 'c' ]console // → [ 'base', 'a', 'b', 'c' ]
store and apply context
Allows storage and restoration of context. Usefull for edge cases where you need to make a handle to a nested field in a certain context
Consists of 2 methods
- applyContext(context)
- storeContext()
const base = key: 'base' a: b: c: 'its c' const instance = key: 'instance'const b = instanceabconst context = bconsole // this will remove the context "instance", and replace it with baseb // will reset the context of b to instance
Apply context can return 3 different types
undefined
Context is restored without any differencesBase
A set has happened in the path leading to the target of apply contextnull
A remove has happened in the path leading to the target of apply co ntext