|
| 1 | +# Refactoring With Hidden Elements |
| 2 | + |
| 3 | +## A Starting Point |
| 4 | + |
| 5 | +```js |
| 6 | +class Counter extends HTMLElement { |
| 7 | + get x() { return this.xValue; } |
| 8 | + set x(value) { |
| 9 | + this.xValue = value; |
| 10 | + window.requestAnimationFrame(this.render.bind(this)); |
| 11 | + } |
| 12 | + |
| 13 | + clicked() { |
| 14 | + this.x++; |
| 15 | + } |
| 16 | + |
| 17 | + constructor() { |
| 18 | + super(); |
| 19 | + this.onclick = this.clicked.bind(this); |
| 20 | + this.xValue = 0; |
| 21 | + } |
| 22 | + |
| 23 | + connectedCallback() { this.render(); } |
| 24 | + |
| 25 | + render() { |
| 26 | + this.textContent = this.x.toString(); |
| 27 | + } |
| 28 | +} |
| 29 | +window.customElements.define('num-counter', Counter); |
| 30 | +``` |
| 31 | + |
| 32 | +## 1. Instance Variables |
| 33 | + |
| 34 | +```js |
| 35 | +class Counter extends HTMLElement { |
| 36 | + var xValue; |
| 37 | + |
| 38 | + get x() { return this->xValue; } |
| 39 | + set x(value) { |
| 40 | + this->xValue = value; |
| 41 | + window.requestAnimationFrame(this.render.bind(this)); |
| 42 | + } |
| 43 | + |
| 44 | + clicked() { |
| 45 | + this.x++; |
| 46 | + window.requestAnimationFrame(this.render.bind(this)); |
| 47 | + } |
| 48 | + |
| 49 | + constructor() { |
| 50 | + super(); |
| 51 | + this.onclick = this.clicked.bind(this); |
| 52 | + this->xValue = 0; |
| 53 | + } |
| 54 | + |
| 55 | + connectedCallback() { this.render(); } |
| 56 | + |
| 57 | + render() { |
| 58 | + this.textContent = this.x.toString(); |
| 59 | + } |
| 60 | +} |
| 61 | +window.customElements.define('num-counter', Counter); |
| 62 | +``` |
| 63 | + |
| 64 | +## 2. Hidden Methods |
| 65 | + |
| 66 | +```js |
| 67 | +class Counter extends HTMLElement { |
| 68 | + var xValue; |
| 69 | + |
| 70 | + hidden get x() { return this->xValue; } |
| 71 | + hidden set x(value) { |
| 72 | + this->xValue = value; |
| 73 | + window.requestAnimationFrame(this->render.bind(this)); |
| 74 | + } |
| 75 | + |
| 76 | + hidden clicked() { |
| 77 | + this->x++; |
| 78 | + window.requestAnimationFrame(this->render.bind(this)); |
| 79 | + } |
| 80 | + |
| 81 | + constructor() { |
| 82 | + super(); |
| 83 | + this->xValue = 0; |
| 84 | + this.onclick = this->clicked.bind(this); |
| 85 | + } |
| 86 | + |
| 87 | + connectedCallback() { this->render(); } |
| 88 | + |
| 89 | + hidden render() { |
| 90 | + this.textContent = this->x.toString(); |
| 91 | + } |
| 92 | +} |
| 93 | +window.customElements.define('num-counter', Counter); |
| 94 | +``` |
| 95 | + |
| 96 | +## 3. Class Initializer Block |
| 97 | + |
| 98 | +```js |
| 99 | +class Counter extends HTMLElement { |
| 100 | + var xValue; |
| 101 | + |
| 102 | + hidden get x() { return this->xValue; } |
| 103 | + hidden set x(value) { |
| 104 | + this->xValue = value; |
| 105 | + window.requestAnimationFrame(this->render.bind(this)); |
| 106 | + } |
| 107 | + |
| 108 | + hidden clicked() { |
| 109 | + this->x++; |
| 110 | + window.requestAnimationFrame(this->render.bind(this)); |
| 111 | + } |
| 112 | + |
| 113 | + constructor() { |
| 114 | + super(); |
| 115 | + this->xValue = 0; |
| 116 | + this.onclick = this->clicked.bind(this); |
| 117 | + } |
| 118 | + |
| 119 | + connectedCallback() { this->render(); } |
| 120 | + |
| 121 | + hidden render() { |
| 122 | + this.textContent = this->x.toString(); |
| 123 | + } |
| 124 | + |
| 125 | + static { |
| 126 | + window.customElements.define('num-counter', this); |
| 127 | + } |
| 128 | +} |
| 129 | +``` |
0 commit comments