Last active
March 20, 2017 16:47
-
-
Save topicus/079227df9ff42a07bb03d88c932e8627 to your computer and use it in GitHub Desktop.
How this is dynamically bind to DOM elements.
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
// 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