Skip to content

Instantly share code, notes, and snippets.

@yairEO
Created February 20, 2019 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yairEO/19fbf1864b96b6ca519832728ba48b28 to your computer and use it in GitHub Desktop.
Save yairEO/19fbf1864b96b6ca519832728ba48b28 to your computer and use it in GitHub Desktop.
deepBind for object methods at any depth with any context
function deepBind(O){
for(var key in O)
// if value is an object, go deeper
if( O[key] instanceof Object && typeof O[key] == 'object' )
deepBind.call(this, O[key])
// look for a value which is a function and bind it
else if( typeof O[key] == 'function' )
O[key] = O[key].bind(this);
}
@yairEO
Copy link
Author

yairEO commented Feb 20, 2019

Usage:

deepBind.call(this, objectWithDeepMethods);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment