Some common javascript functions for working with arrays and functions

the callBack class becomes obsolete with these but all places where it is used are updates
This commit is contained in:
Robin Appelman 2011-01-04 22:30:10 +01:00
parent 7710dc7325
commit 5a05da2af2
1 changed files with 49 additions and 17 deletions

View File

@ -20,11 +20,10 @@
*/
//The callBack object provides an easy way to pass a member of an object as callback parameter and makes sure that the 'this' is always set correctly when called.
//bindScope provides a much cleaner sollution but we keep this one for compatibility and instead implement is with bindScope
callBack=function(func,obj){
this.id=callBack.callBacks.length;
callBack.callBacks[this.id]=this;
this.func=func;
this.obj=obj;
var newFunction=func.bindScope(obj);
callBack.callBacks[this.id]=newFunction;
}
callBack.callBacks=Array();
@ -36,18 +35,6 @@ callBack.call=function(id,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10){
}
}
callBack.prototype=function(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10){
return this.call(false,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10);
}
callBack.prototype.func=false;
callBack.prototype.obj=false;
callBack.prototype.call=function(dummy,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10){
return this.func.call(this.obj,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10);
}
callBack.prototype.apply=function(dummy,arguments){
return this.func.apply(this.obj,arguments);
}
//provide a simple way to add things to the onload
OC_onload=new Object();
@ -198,4 +185,49 @@ loadScript=function(url){//dynamicly load javascript files
script.setAttribute('src',url);
body=document.getElementsByTagName('body').item(0);
body.appendChild(script);
}
}
Function.prototype.bindScope=function(obj){
var o=obj;
var fn=this;
return function(){
return fn.apply(o,arguments);
}
}
Function.prototype.bind=function(){
var args = [];
var fn=this;
for (var n = 0; n < arguments.length; n++){
args.push(arguments[n]);
}
return function (){
var myargs = [];
for (var m = 0; m < arguments.length; m++){
myargs.push(arguments[m]);
}
return fn.apply(this, args.concat(myargs));
};
}
Array.prototype.foreach=function(func,that){
if (!func) return;
that=that||this;
var returns=[];
for(var i=0;i<this.length;i++){
returns.push(func.call(that,this[i]));
}
return returns;
}
Array.prototype.where = function(func,that) {
var found = [];
that=that||this;
for(var i = 0, l = this.length; i < l; ++i) {
var item = this[i];
if(func.call(that,item)){
found.push(item);
}
}
return found;
};