Skip to content

performant object scope function [event] delegation like function.bind or jquery.proxy

Notifications You must be signed in to change notification settings

soenkekluth/delegatejs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DelegateJS

performant object scope function [event] delegation like function.bind or jquery.proxy

Install

npm install delegatejs

Usage

const delegate = require('delegatejs');

class Test {
    
  constructor() {
    window.addEventListener('resize', delegate(this, this.onResize), false);
  }
  
  onResize(e) {
    console.log(this, e);
  }
}

if you also need to remove your listener (what you always should do in statefull apps) you can du so:

const delegate = require('delegatejs');

class Test {
    
  constructor() {
    this.onResize = delegate(this, this.onResize);
    window.addEventListener('resize', this.onResize , false);
  }
  
  onResize(e) {
    console.log(this, e);
  }

  removeResizeListener(){
    window.removeEventListener('resize', this.onResize);
  }
}

About

performant object scope function [event] delegation like function.bind or jquery.proxy

Resources

Stars

Watchers

Forks

Packages

No packages published