xsor
This is a JavaScript module that eases the creation of object property descriptors (also known as "accessors") backed by pseudo-private variables. This saves you from having to mint unique symbols for your "private" data and focus on the logic of your application.
Code like this:
var {}; var x = '__x';var { // logic for when stuff changes}; MyClassprototype = { return thisx; } { if thisx !== value onChangeX; }; // "hide" __x from for..in loopsObject;
becomes:
var {}; var xsor = ;Object
If you're unfamiliar with property descriptors, check out the docs for Object.defineProperty, Object.defineProperties and Object.create.
API
The module exports a top-level function that returns a property descriptor, and can be called in a couple of different ways. Generally speaking, descriptors are
xsor(change)
This is the simplest form, and creates a descriptor that fires the change
function whenever the value of any instance changes.
var xsor = ;var obj = {};Object;objvalue = 1;// logs: "value changed from undefined to 1"
See below for more information on the change handler.
xsor(name [, options])
Creates a descriptor that refernces the private variable name
with the
following accepted options:
initial: value
Declares the initial value for this accessor, which will be returned by the first get.
change: function(value, previous, symbol)
Declares a change handler for the value of each individual instance,
where value
is the new value, previous
is the previous value (or
undefined
, if no initial value was set), and symbol
is the name
of the symbol used to access the private value.
If you return false
from this function, the change will not be
applied.
var xsor = ;var x = {};var { console; return value !== undefined;};Object; xfoo = 1;// logs: "foo changed from undefined to 1"xfoo = 2;// logs: "foo changed from 1 to 2"
parse: function(value, symbol)
Declares a parse function for the input value, which will be called whenever the value is set, but before the change handler. The return value is the parsed or coerced value. For instance, to coerce values to numbers, you could do:
var xsor = ;var assert = ; var {}; Object
enumerable: true
By default, descriptors returned by xsor()
are not enumerable, which means
that they won't be returned by Object.keys()
or iterated over by for..in
loops. You can make them enumerable by passing enumerable: true
.