Additional functions on the JS Function object
| Function | Additional functions on the JS Function object |
| Functions | |
| repeat | executes this function repeatedly, returning results as array |
| bind | returns a version of this function bound to a specific scope object |
| cache | returns a caching version of this function |
| createCallback | returns a callback function that will execute this function with the supplied arguments |
| after | returns a chain function out of this function and another function, new function second |
| before | returns a chain function out of this function and another function, new function first |
| createChainFunction | returns a function that will execute a chain of functions when called. |
| createDelegate | returns a function that executes this function with supplied scope and arguments |
| createSequence | creates and returns a combined function call sequence of this function followed by the passed function. |
| createInterceptor | returns a function that executes a supplied interceptor function, then this function unless the interceptor returns false. |
Function.prototype.repeat = function( count )
executes this function repeatedly, returning results as array
| count | number of times to execute this function |
When this this function is executed, it will be passed 2 parameters: index and count. index is the current iteration number, starting with 0. count is the original count passed to repeat.
//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
Function.prototype.bind = Function.prototype.bind ||function( o )
returns a version of this function bound to a specific scope object
| scope | object 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 |
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.
This function is API compatible with the EcmaScript 5 “bind” function
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")
Function.prototype.cache =function cache()
returns a caching version of this function
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
This will only work properly with functions that do not take parameters.
Do not use this for prototype functions, unless you intend to cache the result across ALL instances of this class. To have chaching 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)
}//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();
Function.prototype.createCallback = function( /*args...*/ )
returns a callback function that will execute this function with the supplied arguments
The purpose of this function is to simplify calling a function with a defined set of paramters.
This function was adapted from Ext 2.0.1 (http://extjs.com)
//old way:
var f= function(){
myFunc("woot!",false);
}
//new way:
var f = myFunc.createCallback("woot!",false);
Function.prototype.after = function( func )
returns a chain function out of this function and another function, new function second
| func | a function to chain after this function. This function may be a chain function |
See createChainFunction for a description of chain functions
Function.prototype.before = function( func )
returns a chain function out of this function and another function, new function first
| func | a function to chain before this function. This function may be a chain function |
See createChainFunction for a description of chain functions
Function.createChainFunction=function( initialChain )
returns a function that will execute a chain of functions when called.
| initialChain | Optional, default [] Array to use as the initial function chain |
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.
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
| exitChain | If 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 |
| lastReturn | The return value from the function that executed before this one in the chain |
| args | An array of the arguments to the next function in the chain. Altering this array will alter the arguments to the next function |
| index | The 0-based position of this function in the chain |
| array | The 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 |
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>");
Function.prototype.createDelegate = function( obj, args, appendArgs )
returns a function that executes this function with supplied scope and arguments
| obj | Optional, default window or $server.globalScope object to use as the “this” scope when the function is executed |
| args | Optional, default[] Array of arguments to call this function with. |
| appendArgs | Optional, default false |
The purpose of this function is to simplify calling a function with a defined set of parameters, and a defined scope.
This function was adapted from Ext 2.0.1 (http://extjs.com)
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!");
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.
| fcn | function to append to this one |
| object | Optional, default window or $server.globalScope The scope of the passed fcn |
This function was adapted from Ext 2.0.1 (http://extjs.com)
var first = function(){
print("I am first<br>")
}
var second = function(){
print("I am second<br>")
}
var seq = first.createSequence(second)
seq();
Function.prototype.createInterceptor=function( fcn, scope )
returns a function that executes a supplied interceptor function, then this function unless the interceptor returns false.
| fcn | function to execute BEFORE this function |
| scope | scope to execute fcn within |
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.
This function was adapted from Ext 2.0.1 (http://extjs.com)
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();executes this function repeatedly, returning results as array
Function.prototype.repeat = function( count )
returns a version of this function bound to a specific scope object
Function.prototype.bind = Function.prototype.bind ||function( o )
returns a caching version of this function
Function.prototype.cache =function cache()
returns a callback function that will execute this function with the supplied arguments
Function.prototype.createCallback = function( /*args...*/ )
returns a chain function out of this function and another function, new function second
Function.prototype.after = function( func )
returns a chain function out of this function and another function, new function first
Function.prototype.before = function( func )
returns a function that will execute a chain of functions when called.
Function.createChainFunction=function( initialChain )
returns a function that executes this function with supplied scope and arguments
Function.prototype.createDelegate = function( obj, args, appendArgs )
creates and returns a combined function call sequence of this function followed by the passed function.
Function.prototype.createSequence = function( fcn, scope )
returns a function that executes a supplied interceptor function, then this function unless the interceptor returns false.
Function.prototype.createInterceptor=function( fcn, scope )
Executes a provided function once per array element.
if ( !Array.prototype.forEach )
Prepends supplied function to the event chain of this object.
Object.prototype.before=function( functionName, functionObj )
Appends supplied function to the event chain of this object.
Object.prototype.after=function( functionName, functionObj )