Last active
December 1, 2018 17:30
-
-
Save rakibulalam/bf4deec719438af16d113fd3be81c5d6 to your computer and use it in GitHub Desktop.
ES6 Auto Bind !!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*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