Created
February 9, 2019 07:34
-
-
Save alejandrolechuga/f4d3ce28a605f53764c2046420a40747 to your computer and use it in GitHub Desktop.
clases en javascript
This file contains 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
/* 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