jfObject
Class to easily manipulate objects. Can be used as base class for event-oriented libraries.
Usage
Objects as parameters
You can pass several objects and will be merged recursively.
const obj = ;console; // { a : { b : 1, c : 3 }, d : { e : { f : 5 } } }
Dot notation
You can use dot notation in order to assign/retrieve/test nested objects.
const obj = ;obj;console; // { e : { f : 5 } }console; // { f : 5 }console; // true
Merging objects
Objects can be merged in 2 ways:
- Recursively: Use
obj.assign(...)
. - Overwrite existing keys: Use
Object.assign(obj, ...)
Using obj.assign
will convert all objects into jfObject
instances.
const obj = a : b : 0 ;// Recursive mergeobj;console; // { a : { b : 0, c : 1 } }// Overwrite mergeObject;console; // { a : { c : 2 } }
Iterating over properties
You can use new ES6
loop for..of
:
const obj = ;for let prop of obj console; // a = 1// c = 2// f = 3// h = 4
Keys & values
If you need to split object into keys and values, you can use split
or toArray
methods.
const obj = ;console; // { keys: [ 'a', 'c', 'f', 'h' ], values: [ 1, 2, 3, 4 ] }console; // [ [ 'a', 1 ], [ 'c', 2 ], [ 'f', 3 ], [ 'h', 4 ] ]
Events
jfObject
instances can emit/listen events because they extend node Events
class:
const obj = ;obj;obj;
Singleton
If you need to share the same instance between differents external modules,
you can use static method i()
in order to retrieve the same instance
anywhere in the app.
// User class methods// Initialize the User instance on ajax response using external library:{ //... User; //...}// In UI, using external framework<user-info data="{{User.i()}}" />