Skip to content

Instantly share code, notes, and snippets.

@rakibulalam
Last active December 1, 2018 17:30
Show Gist options
  • Save rakibulalam/bf4deec719438af16d113fd3be81c5d6 to your computer and use it in GitHub Desktop.
Save rakibulalam/bf4deec719438af16d113fd3be81c5d6 to your computer and use it in GitHub Desktop.
ES6 Auto Bind !!
/*bind your all method autometically in your class*/
/*Author: Md. Rakibulalam , <[email protected]> (http://rakibulalam.info)*/
/*
constructor(){
this._autobind();
}
*/
_autobind(){
Reflect.ownKeys(Reflect.getPrototypeOf(this)).forEach((m)=>{
if(typeof this[m]==='function' && (m!=='_autobind' || m!=='constructor'))
{
this[m]=this[m].bind(this)
}
});
}
/*enjoy!! your code*/
/*ES5*/
'use strict';
_autobind = function _autobind(_obj) {
Reflect.ownKeys(Reflect.getPrototypeOf(_obj)).forEach(function (m) {
if (typeof _obj[m] === 'function' && (m !== '_autobind' || m !== 'constructor')) {
_obj[m] = _obj[m].bind(_obj);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment