Skip to content

Instantly share code, notes, and snippets.

@alejandrolechuga
Created February 9, 2019 07:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alejandrolechuga/f4d3ce28a605f53764c2046420a40747 to your computer and use it in GitHub Desktop.
Save alejandrolechuga/f4d3ce28a605f53764c2046420a40747 to your computer and use it in GitHub Desktop.
clases en javascript
/* jshint esnext: true */
// class ES6
class Button {
constructor(label, callback) {
this.label = label;
this.callback = callback;
this.html = this.createHTML();
this.bindEvents();
}
onClick() {
this.callback();
}
bindEvents() {
this.html.addEventListener('click', this.onClick.bind(this));
}
createHTML() {
let button = document.createElement('button');
button.innerHTML= this.label;
return button;
}
getHTML(){
return this.html;
}
static compare(a, b){
return a.label === b.label;
}
}
class ButtonImage extends Button {
constructor(url, callback) {
super(null, callback);
this.setURL(url);
}
setURL(url){
this.html.src = url;
}
createHTML() {
let img = document.createElement('img');
return img;
}
}
let button = new Button('Click', function (){
console.log('Click !!!');
});
document.body.appendChild(button.getHTML());
let imgBtn = new ButtonImage('https://i.imgur.com/Beo1vHQ.gif', function () {
console.log('Soy la imagen con click');
});
document.body.appendChild(imgBtn.getHTML());
let button2 = new Button('Hola', function (){
console.log('Click !!!');
});
console.log(Button.compare(button, button2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment