Skip to content
This repository has been archived by the owner on May 15, 2023. It is now read-only.

Array & Map Constraints

Stephen Oney edited this page Jan 15, 2014 · 7 revisions

So far, all of the constraint variables we have discussed have been simple objects (constructed using cjs.constraint(). However, constraint variables can also be arrays or objects (maps).

####Arrays cjs.array(arr) creates an Array constraint, which adds a constraint wrapper to all of the standard Array.prototype methods, including .pop(), .push(), etc.

It also includes the special methods:

Example:

var arr = cjs.array({
              value: [1,2,3]
          });
arr.push(4);
arr.length(); // 4
arr.toArray(); // [1,2,3,4]

####Maps cjs.map(obj) creates a Map constraint, which adds a constraint wrapper to a standard object with key/value pairs. It contains a number of methods, including:

Example:

var m = cjs.map({
            value: {x: 1, y: 2}
        });
m.item('x'); // 1
m.item('z', 3);
m.keys(); // ['x','y','z']

Next: States & FSMs

Previous: Detecting Variable Changes

Clone this wiki locally