Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. // Automatically binds ES6 class methods
  2. // From within constructor() call classAutoBind(this)
  3.  
  4. window.classAutoBind = function(context) {
  5. for (let method of Object.getOwnPropertyNames(Object.getPrototypeOf(context))) {
  6. if (method !== 'constructor') {
  7. context[method] = context[method].bind(context)
  8. }
  9. }
  10. }
  11.  
  12. // Example:
  13. class Tacos {
  14. constructor() {
  15. this.type = 'Tacos'
  16. classAutoBind(this)
  17. }
  18. getType() {
  19. return this.type
  20. }
  21. }
  22.  
  23. var tacos = new Tacos()
  24. console.log(tacos.getType())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement