Utils.Object Class
Provides a whole bunch of utility functions
Methods
delegate
-
to -
from -
methods
Delegates method invocations from one object to another. The first argument is the receiving object, the second one is the providing object
Parameters:
-
toObjectReceiving object
-
fromObjectProviding object
-
methodsArrayArray of method names to delegate
Returns:
The receiving object
Example:
var oRec = {
name: 'Test'
};
var oProv = {
upperName: function () {
return this.name.toUpperCase();
}
};
Utils.delegate(oRec, oProv, ['upperName']);
oRec.upperName();
// -> "TEST"
exec
-
f
Creates a predefined method invocation and allows to skip the receiver of the invocation. All arguments after the first parameter are considered to be the parameters which shall be used (partial application)
Parameters:
-
fString | FunctionMethod to invoke
Returns:
Function which accepts a receiver and optional arguments
Example:
// general uppercase function
var upper = Utils.exec('toUpperCase');
upper('hello world'); // -> 'HELLO WORLD'
// general splitter function for spaces
var splitSpace = Utils.exec('split', ' ');
splitSpace('hello world'); // -> ['hello', 'world']
// splitter a bit more general
var split = Utils.exec('split');
// pass parameters later
split('hello world', ' '); // -> ['hello', 'world']
// if a function returns undefined/void, the receiver is
// returned
// ---
var preventDefault = Utils.exec('preventDefault');
var handleEvent = Utils.pipe(preventDefault, console.log.bind(console));
document.querySelector('a').addEventListener('click', handleEvent);
// logs the Event object on each click while preventing the default
// action
extend
-
base -
ext
Merges the right object into the left one and returns the left one
Parameters:
-
baseObjectBasic object which should be extended
-
extObjectExtending object
Returns:
Returns the base
Example:
import {extend} from './utils/object';
var obj1 = {foo: 'bar'};
var obj2 = {goo: 'baz'};
extend(obj1, obj2);
// -> {foo: 'bar', goo: 'baz'}
forward
-
from -
to -
methods
Forwards method invocations from one object to another. The first argument is the forwarding object, the second one is the receiving object. This helps for example to create private properties
Parameters:
-
fromObjectForwarding object
-
toObjectReceiving object
-
methodsArrayArray of method names to forward
Returns:
The forwarding object
Example:
var oForw = {};
var oProv = {
name: 'Test',
upperName: function () {
return this.name.toUpperCase();
}
};
Utils.forward(oForw, oProv, ['upperName']);
oForw.upperName();
// -> "TEST"
oForw.name;
// -> undefined
has
-
prop -
obj
Checks if a field exists on a given object and is not inherited
Parameters:
-
propStringName of the field
-
objObjectThe object to test against
Returns:
True if field exists, false otherwise
Example:
import {has} from './utils/object';
var obj = {foo: 'bar'};
has('foo', obj);
// -> true
inherit
-
base -
extension
Works like Utils.extend() but for constructor functions
Parameters:
-
baseFunctionConstructor of inherited class
-
extensionObjectExtension object
Returns:
Inherited prototype object
Example:
var Base = function () {};
var Ext = function () {};
Ext.prototype = Utils.inherit(Base, {
// definition
});
keys
-
obj
Returns the names of all fields which exist on a given object
Parameters:
-
objObjectThe object to retrieve the fields from
Returns:
List of fields
Example:
import {keys} from './utils/object';
var obj = {foo: 'bar'};
keys(obj);
// -> ['foo']
mixin
-
base -
extensions
Merges all right objects into the left one and returns the left one
Parameters:
-
baseObjectBasic object which should be extended
-
extensionsArrayList of extension objects to mix into
Returns:
Returns the base
Example:
var base = {foo: 'bar'};
var ext1 = {goo: 'baz'};
var ext2 = {hoo: 'wicked'};
Utils.mixin(base, [ext1, ext2]);
base;
// -> {foo: 'bar', goo: 'baz', hoo: 'wicked'}
pairs
-
obj
Converts the fields of a given object into a multi dimensional list of key-value pairs
Parameters:
-
objObjectThe object to retrieve pairs from
Returns:
List of lists of key-value pairs
Example:
import {pairs} from './utils/object';
var obj = {foo: 'bar'};
pairs(obj);
// -> [['foo', 'bar']]
prop
-
prop -
obj
Gets a property from a given object
Parameters:
-
propStringThe property to get
-
objAnyThe object/value to get the property from
Returns:
Value of the property
Example:
import {prop} from './utils/object';
var obj = {foo: 'bar'}
prop('foo', obj); // -> 'bar'
const getFoo = prop('foo');
getFoo(obj); // -> 'bar'
values
-
obj
Returns the values of every field on a given object which is not inherited
Parameters:
-
objObjectThe object to retrieve the values from
Returns:
List of values
Example:
import {values} from './utils/object';
var obj = {foo: 'bar'};
values(obj);
// -> ['bar']
