Chip Utils
Utility methods used in some of the libraries created with Chip.
Getting Started
To use these utilites in your own project install using npm.
npm install chip-utils
Class
This utility provides syntactic sugar on normal JavaScript class inheritance supporting additional features such as getters/setters and static inheriance.
To set up class inheritance, extend from the generic Class object. Then your classes will have an extend
method and
can use it to extend to subclasses.
var Class = ; { } Class; { } MyClass;
Static properties will also be extended. You may use the static
member property as a shortcut to defining statics, but
this is not necessary.
// Parent class { if data Object; } Resourceuri = '/api/v1'Resource { var ResourceType = this; return $;}; Class; { Resource;} Resource; // Define this AFTER calling extend writes itPersonuri += '/people' Person // returns a promise full of Person objects
Using the static
member property:
Resource;
Mixins or multiple inhertiance allows for adding methods from multiple classes.
// Person will inherit from Resource, but will also get the methods from EventTarget { // Calling both constructors to ensure we are initialized correctly Resource; EventTarget;} Resource; var p = ;p;
The extended mixins and the prototype of the class can safely use getters/setters.
{ thisfirstName = firstName; thislastName = lastName;} Class; { Person; thisnickName = nickName;} Person; var rando = 'John' 'Smith';var friend = 'Robert' 'Haroldson' 'Bob';console; // John Smithconsole; // Bobfriendname = 'Rob';console; // Rob
EventTarget
This utility ties into the browser eventing system and exposes it for your libraries to use. This does not work in
node.js. EventTarget
extends from Class
so anything extending it will take advantage of the extension system. Or it
may be used as a mixin as described above, just be sure to call the constructor within your subclass' constructor.
To extend the event target:
var EventTarget = ; { EventTarget;} EventTarget;
To listen to events on an event target (or remove listeners:
var obj = ; obj; // Will only get called onceobj; // `on` and `off` are aliases of `addEventListener` and `removeEventListener`obj;obj;obj;obj;
Note
This EventTarget class will only work in the browser (or jsdom) since it uses a native EventTarget to work with the browser's eventing system. It does not support bubbling.