Skip to content

Instantly share code, notes, and snippets.

@topicus
Last active March 20, 2017 16:47
Show Gist options
  • Save topicus/079227df9ff42a07bb03d88c932e8627 to your computer and use it in GitHub Desktop.
Save topicus/079227df9ff42a07bb03d88c932e8627 to your computer and use it in GitHub Desktop.
How this is dynamically bind to DOM elements.
// Suppose you have an html like:
// <div id="like-button">
// <i class"icon-hand" />
// <div>Like</div>
// </div>
function LikeButton(id) {
this.buttonId = id;
this.subscribe = function() {
let btn = document.getElementById(this.buttonId);
btn.addEventListener('click', function() {
console.log(this); // Points to the event.target
});
}
}
let btn = new LikeButton('like-button');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment