Function

Methods

bind(thisArg) → {function}

Creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

Parameters:

NameTypeDescription
thisArgfunction

The value to be passed as the this parameter to the target function when the bound function is called.

Returns:

The bound function.

bindTo() → {function}

Alias for function.bind.

Returns:

The bound function.

defer() → {Number}

Alias for function.delay.

Returns:

The numerical ID of the timeout.

delay(delay, thisArg, args) → {Number}

Delays execution of the function by the given delay.

Parameters:

NameTypeDescription
delayNumber

Time to delay in milliseconds.

thisArgObject

Object to use for this when calling the function.

argsArray

Arguments to use when calling the function.

Returns:

The numerical timeout ID of the delayed function.

pass(arg, thisArg) → {function}

Creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

Parameters:

NameTypeArgumentDefaultDescription
argArrayoptional []

Arguments to prepend to the bound function.

thisArgObject

The value to be passed as the this parameter to the target function when the bound function is called.

Returns:

The newly bound function.

periodical(periodical, scope, args) → {Number}

Periodically call the function with the given scope and arguments.

Parameters:

NameTypeDescription
periodicalNumber

Number of milliseconds to wait before each call to function.

scopeObject

Scope to use when invoking function.

argsArray

Arguments to use when calling the function.

Returns:

The numerical ID of the interval.

subscribeOnce(publisher, eventTypes, scope) → {function}

Subscribes a method to a list of events. Each event subscribed will only be fired once, after which it is unsubscribed.

Parameters:

NameTypeArgumentDescription
publisherClass

Object or Class on which you want to register the method.

eventTypesString | Array

String or array containing eventTypes to which to subscribe this method to.

scopeObjectoptional 

Scope in which the method will run when the event fires.

Returns:

Method as subscribed.

subscribeTo(publisher, eventTypes, scope) → {function}

Subscribes a method to a list of events

Parameters:

NameTypeArgumentDescription
publisherClass

Object or Class on which you want to register the method.

eventTypesString | Array

String or array containing eventTypes to which to subscribe this method to.

scopeObjectoptional 

Scope in which the method will run when the event fires.

Returns:

Method as subscribed.

unsubscribeFrom(publisher, eventTypes) → {function}

Unsubscribes a method from a list of events

Parameters:

NameTypeDescription
publisherClass

Object or Class on which you want to unregister the method.

eventTypesString | Array

String or array containing eventTypes from which to unsubscribe this method to.

Returns:

Method as unsubscribed.

Example

var differentScope = new new MAF.Class({ // Create and initialize a new MAF.Class in one go.
   version: 123
 });

 var publisher = new new MAF.Class({  // Create and initialize a new MAF.Class in one go.
   version: 321
 });

 var fnOriginal = function (ev) {
    switch (ev.type) {
       case 'withScope':
          console.log('Running in differentScope scope, version: ', this.version);
          break;
       case 'noScope':
          console.log('Running in publisher scope, version: ', this.version);
          break;
       }
 };

 //If you subscribed with using a scope, you have to unsubscribe using the returned method.
 var fnSubscribed = fnOriginal.subscribeTo(publisher, ['withScope'], differentScope);
 fnOriginal.subscribeTo(publisher, ['noScope']);

 publisher.fire('withScope');
 publisher.fire('noScope');
 fnSubscribed.unsubscribeFrom(publisher, ['withScope']);
 fnOriginal.unsubscribeFrom(publisher, ['noScope']);