Skip to content

Instantly share code, notes, and snippets.

@sketchpunk
Created April 22, 2016 14:43
Show Gist options
  • Save sketchpunk/e2e342124dc3db6bf5ec5aa5c66b1a30 to your computer and use it in GitHub Desktop.
Save sketchpunk/e2e342124dc3db6bf5ec5aa5c66b1a30 to your computer and use it in GitHub Desktop.
Create a delegate function that keeps the reference of the parent object of the function.
function Delegate(obj,funcRef){
var args = null;
if(arguments.length > 2){ args = []; for(var i=2; i < arguments.length; i++) args.push(arguments[i]); }
return function(){
if(args == null) funcRef.apply(obj,arguments);
else{
var params = args.slice(); //Need to make a copy of args, When just adding to args caused a problem with bubbling events.
for(var i=0; i < arguments.length; i++) params.push(arguments[i]);
funcRef.apply(obj,params);
}//if
};
}//func
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment