Function

Additional functions on the JS Function object

Summary
FunctionAdditional functions on the JS Function object
Functions
repeatexecutes this function repeatedly, returning results as array
bindreturns a version of this function bound to a specific scope object
cachereturns a caching version of this function
createCallbackreturns a callback function that will execute this function with the supplied arguments
afterreturns a chain function out of this function and another function, new function second
beforereturns a chain function out of this function and another function, new function first
createChainFunctionreturns a function that will execute a chain of functions when called.
createDelegatereturns a function that executes this function with supplied scope and arguments
createSequencecreates and returns a combined function call sequence of this function followed by the passed function.
createInterceptorreturns a function that executes a supplied interceptor function, then this function unless the interceptor returns false.

Functions

repeat

Function.prototype.repeat = function(count)

executes this function repeatedly, returning results as array

Parameters

countnumber of times to execute this function

Detail

When this this function is executed, it will be passed 2 parameters: index and countindex is the current iteration number, starting with 0.  count is the original count passed to repeat.

Examples

//pre-defined function
var f = function(index,count){
$res.print("loop #" + index + " of " +count+"<br>");
}
f.repeat(10);

//anonymous function
//The Object keyword is only necessary when not assigning the result
Object(function(index,count){
$res.print("loop #" + index + " of " +count+"<br>");
}).repeat(10);

//building results
var array = ((function(index,count){
return(index +","+count)
}).repeat(10))

See also: Array.forEach

bind

Function.prototype.bind = Function.prototype.bind ||function(o)

returns a version of this function bound to a specific scope object

Parameters

scopeobject to bind as “this”
n1+any arguments after scope will be bound to the function as well.  Any arguments passed to the returned function will appended to these when calling the returned function

Detail

The purpose of this function is to bind a function to a specific scope.  If this function is later assigned to another object, it will still apply to it’s bound scope.

Note

This function is API compatible with the EcmaScript 5 “bind” function

Example

var obj={
name"bob"
}
var f = function(){
Myna.println(this.name)
}

var bound =f.bind(obj);

bound(); //prints bob
f();//throws an error

//example using bound arguments

var logError = Myna.log.bind({},"ERROR");
logError("something bad happend")

cache

Function.prototype.cache =function cache()

returns a caching version of this function

Detail

The purpose of this function is to create a lazy-loading versiom of this function.  The first time this function is called, the original function is executed, and subsequent calls immediately return the cached value.  If this function takes a single param that can be converted to a string, then that will be used a s a cache key, allowing multiple values to be cached

Note

This will only work properly with functions that do not take parameters.

Warning

Do not use this for prototype functions, unless you intend to cache the result across ALL instances of this class.  To have caching per instance, you can set this in the constructor function, or you can create a lazy loading version like this:

var myClass = function(){
//define in constructor
this.getEmployee=(function(empId){
...do stuff...
}).cache()
}

//or use lazy-load method
myClass.prototype.getManager=function(managerId){
this.getManager =(function(managerId){
...do stuff...
}).cache()
return this.getManager(managerId)
}

Example

//old way:

var f= function getEmployee(empId){
var my = arguments.callee
if (!(empId in my)){
my[empId] = dm.getManager("employees").getById(empId);
= value;
}
return my[empId];
}

//new way:
var f = (function(empIds){
return dm.getManager("employees").getById(empId);
}).cache();

createCallback

Function.prototype.createCallback = function(/*args...*/)

returns a callback function that will execute this function with the supplied arguments

Detail

The purpose of this function is to simplify calling a function with a defined set of paramters.

Note

This function was adapted from Ext 2.0.1 (http://extjs.com)

Example

//old way:
var f= function(){
myFunc("woot!",false);
}

//new way:
var f = myFunc.createCallback("woot!",false);

after

Function.prototype.after = function(func)

returns a chain function out of this function and another function, new function second

Parameters

funca function to chain after this function.  This function may be a chain function

See createChainFunction for a description of chain functions

before

Function.prototype.before = function(func)

returns a chain function out of this function and another function, new function first

Parameters

funca function to chain before this function.  This function may be a chain function

See createChainFunction for a description of chain functions

createChainFunction

Function.createChainFunction=function(initialChain)

returns a function that will execute a chain of functions when called.

Parameters

initialChainOptional, default [] Array to use as the initial function chain

Detail

This creates a function that will execute a chain of functions stored in the returned function’s chainArray property.  Before each function is called a chain property will be set on the function with metadata about the function chain.  See “Chain Object Properties” below.  The chain object can be used to manipulate the chain by altering the return value from previous functions in the chain, altering the arguments to the next function in the chain, or by exiting early via chain.exit().  The final result of the function chain will be returned by the chain function.

Note

The same “chain” property is passed to each function in the chain and thus provides a mechanism for earlier functions in the chain to communicate with later functions in the chain

Chain Object Properties

exitChainIf set to true, then no other functions in the chain will be executed and the return from this function will be the final one.  See the exit function for combining this setting with a return
lastReturnThe return value from the function that executed before this one in the chain
argsAn array of the arguments to the next function in the chain.  Altering this array will alter the arguments to the next function
indexThe 0-based position of this function in the chain
arrayThe complete array of functions in this chain
exit(retval)a function that takes a return value and sets exitChain to true and returns retval as the final value

Example

var obj={
stuff:function (text){
Myna.println("in orig")
return text + " " + this.myVal;
},
myVal:"firstObj"
}

var obj2={
myVal:"secondObj"
}


obj.stuff=obj.stuff.before(function(text){
var chain = arguments.callee.chain;
Myna.println("in before")
chain.args[0] = "before " + text
if (text == "dude!"){
// exit now with this return value, nothing after will be executed
chain.exit("sweet!")
}

})

obj.stuff=obj.stuff.after(function(text){
var chain = arguments.callee.chain;
Myna.println("in after")
return chain.lastReturn + " after "
})

Myna.println(obj.stuff("woot!") +"<hr>");
Myna.println(obj.stuff("dude!") +"<hr>");

obj2.stuff = obj.stuff;
Myna.println(obj2.stuff("woot!") +"<hr>");

See Also

createDelegate

Function.prototype.createDelegate = function(obj,
args,
appendArgs)

returns a function that executes this function with supplied scope and arguments

Parameters

objOptional, default window or $server.globalScope object to use as the “this” scope when the function is executed
argsOptional, default[] Array of arguments to call this function with.
appendArgsOptional, default false
  • By default, if args is defined, then when this delegate is called, any passed arguments will be ignored.
  • If appendArgs is a boolean true (not just a boolean equivalent), then when this function is called, any passed arguments will used, and args will be appended to the passed in arguments.
  • If appendArgs is a number, then args will be inserted into the passed arguments at the indicated position.  For example, a value of 0 would cause args to be placed before the passed in arguments instead of after them.

Detail

The purpose of this function is to simplify calling a function with a defined set of parameters, and a defined scope.

Note

This function was adapted from Ext 2.0.1 (http://extjs.com)

Example

    var a={
myVal:20,
myFunc:function(label,otherVal){
Myna.println("<br>label:" + label + "<br>myVal:" + this.myVal
+ "<br>otherVal:" + otherVal + "<br>");
}
}
var b;

// Problem:
// Can't set default values, and a's function executes against b's properties
b={
myVal:10,
myFunc:a.myFunc
}
b.myFunc("Doh!");

// classic solution:
b={
myVal:10,
myFunc:function(label){
var args = [label,"calling from b"]

a.myFunc.apply(a,args);
}
}
b.myFunc("woot!");

// with createDelegate:
// appends "calling form b" to the arguments passed to myFunc
b={
myVal:10,
myFunc:a.myFunc.createDelegate(a,["calling from b"],true)
}
b.myFunc("woot! woot!");

createSequence

Function.prototype.createSequence = function(fcn,
scope)

creates and returns a combined function call sequence of this function followed by the passed function.  The resulting function returns the results of the orginal function.

Parameters

fcnfunction to append to this one
objectOptional, default window or $server.globalScope The scope of the passed fcn

Note

This function was adapted from Ext 2.0.1 (http://extjs.com)

Example

var first = function(){
print("I am first<br>")
}
var second = function(){
print("I am second<br>")
}
var seq = first.createSequence(second)
seq();

createInterceptor

Function.prototype.createInterceptor=function(fcn,
scope)

returns a function that executes a supplied interceptor function, then this function unless the interceptor returns false.

Parameters

fcnfunction to execute BEFORE this function
scopescope to execute fcn within

Detail

The passed fcn is called before the this function.  If it returns a real boolean false (not null or undefined) then this function will not be executed.  This should only be used on functions that don’t noramally return a value, such as event functions.

Note

This function was adapted from Ext 2.0.1 (http://extjs.com)

Example

var doEmployeeStuff = function(){
print("Doing useful stuff<br>")
}
var doManagerStuff = function(){
print("Intercepted! Doing manager stuff instead!<br>")
return false;
}
var doWork = doEmployeeStuff.createInterceptor(doManagerStuff)
doWork();
Function.prototype.repeat = function(count)
executes this function repeatedly, returning results as array
Function.prototype.bind = Function.prototype.bind ||function(o)
returns a version of this function bound to a specific scope object
Function.prototype.cache =function cache()
returns a caching version of this function
Function.prototype.createCallback = function(/*args...*/)
returns a callback function that will execute this function with the supplied arguments
Function.prototype.after = function(func)
returns a chain function out of this function and another function, new function second
Function.prototype.before = function(func)
returns a chain function out of this function and another function, new function first
Function.createChainFunction=function(initialChain)
returns a function that will execute a chain of functions when called.
Function.prototype.createDelegate = function(obj,
args,
appendArgs)
returns a function that executes this function with supplied scope and arguments
Function.prototype.createSequence = function(fcn,
scope)
creates and returns a combined function call sequence of this function followed by the passed function.
Function.prototype.createInterceptor=function(fcn,
scope)
returns a function that executes a supplied interceptor function, then this function unless the interceptor returns false.
if (!Array.prototype.forEach)
Executes a provided function once per array element.
Object.prototype.before=function(functionName,
functionObj)
Prepends supplied function to the event chain of this object.
Object.prototype.after=function(functionName,
functionObj)
Appends supplied function to the event chain of this object.