can-reflect
Perform operations and read information on unknown data types.
Object
The can-reflect
package exports an object with
methods used to perform operations and read information on unknown data
types. For example the sets the name
property on some type of map:
const canReflect = require("can-reflect");
const map = CREATE_MAP_SOMEHOW();
canReflect.setKeyValue(map,"name", "my map");
Any object can interact with the reflect APIs by having the right can-symbol symbols. The following creates an object that informs how getKeyValue and setKeyValue behave:
const canSymbol = require("can-symbol");
const obj = {
_data: new Map()
};
obj[canSymbol.for("can.getKeyValue")] = function(key){
return this._data.get(key);
};
obj[canSymbol.for("can.setKeyValue")] = function(key, value){
return this._data.set(key, value);
};
can-reflect
organizes it's methods into the following groups:
- Type Reflections - Determine if an object matches a familiar type to CanJS.
- Get/Set Reflections - Read and write to objects.
- Call Reflections - Call functions and function-like objects.
- Shape Reflections - Perform operations based on multiple values within an object.
- Observable Reflections - Listen to changes in observable objects.