API Docs for:
Show:

Utils.Object Class

Provides a whole bunch of utility functions

Item Index

Methods

delegate

(
  • to
  • from
  • methods
)
Object deprecated

Defined in development/js/utils/object.js:204

Deprecated: This is likely to be removed in the future

Delegates method invocations from one object to another. The first argument is the receiving object, the second one is the providing object

Parameters:

  • to Object

    Receiving object

  • from Object

    Providing object

  • methods Array

    Array of method names to delegate

Returns:

Object:

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
)
Function

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:

  • f String | Function

    Method to invoke

Returns:

Function:

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
)
Object

Merges the right object into the left one and returns the left one

Parameters:

  • base Object

    Basic object which should be extended

  • ext Object

    Extending object

Returns:

Object:

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
)
Object

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:

  • from Object

    Forwarding object

  • to Object

    Receiving object

  • methods Array

    Array of method names to forward

Returns:

Object:

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
)
Boolean

Checks if a field exists on a given object and is not inherited

Parameters:

  • prop String

    Name of the field

  • obj Object

    The object to test against

Returns:

Boolean:

True if field exists, false otherwise

Example:

import {has} from './utils/object';

var obj = {foo: 'bar'};

has('foo', obj);
// -> true

inherit

(
  • base
  • extension
)
Object

Works like Utils.extend() but for constructor functions

Parameters:

  • base Function

    Constructor of inherited class

  • extension Object

    Extension object

Returns:

Object:

Inherited prototype object

Example:

var Base = function () {};

var Ext = function () {};
Ext.prototype = Utils.inherit(Base, {
    // definition
});

keys

(
  • obj
)
Array

Returns the names of all fields which exist on a given object

Parameters:

  • obj Object

    The object to retrieve the fields from

Returns:

Array:

List of fields

Example:

import {keys} from './utils/object';

var obj = {foo: 'bar'};

keys(obj);
// -> ['foo']

mixin

(
  • base
  • extensions
)
Object

Merges all right objects into the left one and returns the left one

Parameters:

  • base Object

    Basic object which should be extended

  • extensions Array

    List of extension objects to mix into

Returns:

Object:

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
)
Array

Converts the fields of a given object into a multi dimensional list of key-value pairs

Parameters:

  • obj Object

    The object to retrieve pairs from

Returns:

Array:

List of lists of key-value pairs

Example:

import {pairs} from './utils/object';

var obj = {foo: 'bar'};

pairs(obj);
// -> [['foo', 'bar']]

prop

(
  • prop
  • obj
)
Any

Gets a property from a given object

Parameters:

  • prop String

    The property to get

  • obj Any

    The object/value to get the property from

Returns:

Any:

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
)
Array

Returns the values of every field on a given object which is not inherited

Parameters:

  • obj Object

    The object to retrieve the values from

Returns:

Array:

List of values

Example:

import {values} from './utils/object';

var obj = {foo: 'bar'};

values(obj);
// -> ['bar']