An object which delegates actions on it to other objects

Example


Constructors

Properties

Methods

Constructors

  • Creates a new One instance for propagating operations to all the items in many.

    Parameters

    • many: any[]

      The many objects or functions this One will delegate to.

    • Optional recursive: boolean

      Whether to wrap the arrays returned by get with another One.

    • Optional context: any[]

      An optional shared context to be passed to all propagated method or function calls. This is an array of objects passed as the final arguments in calls. Empty array by default.

    • Optional ctor: OneConstructor

      The constructor used to create the get Ones. This parameter is used internally; no need to supply an argument.

    Returns One

    Example

    const loginYes = new One([username => profileView(username)]);
    loginYes.call([[username]]);

Properties

context?: any[]

The context shared by the many functions or methods of the objects in many. They all receive its items as their last set of arguments.

The constructor function used for creating new 'One's in calls to get.

many: any[]

The many objects this One delegates to.

recursive?: boolean

Whether this One will return other 'One's in calls to get.

Methods

  • Calls all the items in many (if method is not specified) or their corresponding methods (if method is specified). All the calls will receive any items in this.context as their final arguments to enable communication.

    args can be specified as follows: [[a1, a2], [a1, a2], [a1, a2]]

    If this.many has 3 items, they will receive their own args. If there are more items in this.many, they will all get the last provided args array (here the one passed to the third item).

    The one function wraps created 'One's with a proxy to allow methods to be called directly on them. Assuming we want to pass the same args as above to such a method, the call will look like:

    object.method([a1, a2], [a1, a2], [a1, a2]).

    There is no need to wrap with the outer array in such cases.

    Call returns an array containing the return values of the individual calls to many items.

    Parameters

    • Optional args: any[]

      The function or method arguments

    • Optional method: string | number | symbol

      The name of a method to call. A function call is assumed if not specified.

    • Optional ignoreContext: boolean

      Set this to a truthy value to prevent the shared context from getting passed in this call.

    Returns any[]

    Example

    const loginYes = new One([username => profileView(username)]);
    loginYes.call([[username]]);
  • Delete the property from all objects in many.

    Parameters

    • prop: string | number | symbol

    Returns void

    Example

    const o = new One([{a: 1}, {a: 2}])
    o.delete('a');
  • Gets corresponding properties from all the objects in many. If this is a recursive One and forceArray is falsy, the array result will be used as the 'many' argument in a call to this.ctor and the created One is returned instead of the array.

    Parameters

    • Optional prop: string | number | symbol
    • Optional forceArray: boolean

    Returns any[] | One

    Example

    const o = new One([{a: 1}, {a: 2}])
    o.get('a'); // [1, 2]
  • Sets corresponding property values in the objects in many. 'values' are treated similarly to 'args' in the call method.

    Parameters

    • Optional prop: string | number | symbol
    • Optional values: any[]

    Returns any

    Example

    const o = new One([{a: 1}, {a: 2}])
    o.set('a', [4, 7]);