avr39ripe

jsButtonClassExample

Mar 18th, 2021 (edited)
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Study</title>
  6. </head>
  7. <body>
  8.     <script>
  9.         'use strict'
  10.  
  11.         class Button {
  12.             constructor(name, width, height) {
  13.                 this._name = name;
  14.                 this._styles = [{ name: 'width', value: width }, { name: 'height', value: height }];
  15.             }
  16.  
  17.             getName() { return this._name; }
  18.             setName(name) { this._nmae = name; }
  19.             _getStyle(styleName) {
  20.                 return this._styles.find((it) => {
  21.                     if (it.name == styleName) {
  22.                         return it;
  23.                     }
  24.                 })
  25.             }
  26.             _setStyle(styleName, value) { this._getStyle(styleName).value = value; }
  27.             getSize() {
  28.                 return { width: this._getStyle('width').value, heigth: this._getStyle('height').value };
  29.             }
  30.             setSize(width, height) {
  31.                 this._setStyle('width', width);
  32.                 this._setStyle('height', height);
  33.             }
  34.             toStyle() {
  35.                 return this._styles.reduce((prev, it) => {
  36.                     return `${prev}${it.name}: ${it.value}; `
  37.                 }
  38.                     , '')
  39.             }
  40.             toHtml() {
  41.                 return `<button style="${this.toStyle()}">${this._name}</button>`;
  42.             }
  43.             render() { document.writeln(this.toHtml()); }
  44.  
  45.         }
  46.  
  47.         class BootstrapButton extends Button {
  48.             constructor(name, width, height, color) {
  49.                 super(name, width, height);
  50.                 this._styles.push({ name: 'color', value: color });
  51.             }
  52.             getColor() { return this._getStyle('color').value; }
  53.             setColor(color) { this._setStyle('color', color); }
  54.         }
  55.  
  56.         {
  57.             let btn1 = new Button('Push ME!', '80px', '20px');
  58.             btn1.render();
  59.  
  60.             let btn2 = new Button('DO NOT TOUCH ME!', '150px', '40px');
  61.             btn2.render();
  62.  
  63.             let bbtn1 = new BootstrapButton('Colored!', '100px', '40px', 'red');
  64.             bbtn1.render();
  65.         }
  66.     </script>
  67. </body>
  68. </html>
Add Comment
Please, Sign In to add comment