回答編集履歴
2
追記が蛇足だったので取り消し
test
CHANGED
@@ -101,127 +101,3 @@
|
|
101
101
|
proxiedTarget.handleEvent({ type: 'focus' })
|
102
102
|
|
103
103
|
```
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
---
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
追記(汎用化?)
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
```javascript
|
116
|
-
|
117
|
-
const Target = class {
|
118
|
-
|
119
|
-
#element
|
120
|
-
|
121
|
-
#active
|
122
|
-
|
123
|
-
constructor(element) {
|
124
|
-
|
125
|
-
this.#element = element
|
126
|
-
|
127
|
-
element.addEventListener('focus', this)
|
128
|
-
|
129
|
-
element.addEventListener('blur', this)
|
130
|
-
|
131
|
-
}
|
132
|
-
|
133
|
-
get element() {
|
134
|
-
|
135
|
-
return this.#element
|
136
|
-
|
137
|
-
}
|
138
|
-
|
139
|
-
get active() {
|
140
|
-
|
141
|
-
return !!this.#active
|
142
|
-
|
143
|
-
}
|
144
|
-
|
145
|
-
set active(bool) {
|
146
|
-
|
147
|
-
this.#active = bool
|
148
|
-
|
149
|
-
}
|
150
|
-
|
151
|
-
handleEvent({ type }) {
|
152
|
-
|
153
|
-
console.log(type + ' before', this.active)
|
154
|
-
|
155
|
-
this.active = type === 'focus'
|
156
|
-
|
157
|
-
console.log(type + ' after', this.active)
|
158
|
-
|
159
|
-
}
|
160
|
-
|
161
|
-
}
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
const handler = {
|
166
|
-
|
167
|
-
get(target, key, receiver) {
|
168
|
-
|
169
|
-
return target[ key ]
|
170
|
-
|
171
|
-
},
|
172
|
-
|
173
|
-
set(target, key, value, receiver) {
|
174
|
-
|
175
|
-
console.log('YES!', { target, key, value, receiver }) //何らかの処理
|
176
|
-
|
177
|
-
target[ key ] = value
|
178
|
-
|
179
|
-
return true
|
180
|
-
|
181
|
-
}
|
182
|
-
|
183
|
-
}
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
const proxied = target => class extends target {
|
188
|
-
|
189
|
-
constructor(...arg) {
|
190
|
-
|
191
|
-
super(...arg)
|
192
|
-
|
193
|
-
const proxiedTarget = new Proxy(this, handler)
|
194
|
-
|
195
|
-
typeof this.handleEvent === 'function' && (this.handleEvent = this.handleEvent.bind(proxiedTarget))
|
196
|
-
|
197
|
-
return proxiedTarget
|
198
|
-
|
199
|
-
}
|
200
|
-
|
201
|
-
}
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
const ProxiedTarget = proxied(Target)
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
const element = document.body.appendChild(document.createElement('textarea'))
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
const proxiedTarget = new ProxiedTarget(element)
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
proxiedTarget.active = !proxiedTarget.active
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
proxiedTarget.active = !proxiedTarget.active
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
proxiedTarget.handleEvent({ type: 'focus' })
|
226
|
-
|
227
|
-
```
|
1
コードの追加
test
CHANGED
@@ -101,3 +101,127 @@
|
|
101
101
|
proxiedTarget.handleEvent({ type: 'focus' })
|
102
102
|
|
103
103
|
```
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
---
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
追記(汎用化?)
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
```javascript
|
116
|
+
|
117
|
+
const Target = class {
|
118
|
+
|
119
|
+
#element
|
120
|
+
|
121
|
+
#active
|
122
|
+
|
123
|
+
constructor(element) {
|
124
|
+
|
125
|
+
this.#element = element
|
126
|
+
|
127
|
+
element.addEventListener('focus', this)
|
128
|
+
|
129
|
+
element.addEventListener('blur', this)
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
get element() {
|
134
|
+
|
135
|
+
return this.#element
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
get active() {
|
140
|
+
|
141
|
+
return !!this.#active
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
set active(bool) {
|
146
|
+
|
147
|
+
this.#active = bool
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
handleEvent({ type }) {
|
152
|
+
|
153
|
+
console.log(type + ' before', this.active)
|
154
|
+
|
155
|
+
this.active = type === 'focus'
|
156
|
+
|
157
|
+
console.log(type + ' after', this.active)
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
const handler = {
|
166
|
+
|
167
|
+
get(target, key, receiver) {
|
168
|
+
|
169
|
+
return target[ key ]
|
170
|
+
|
171
|
+
},
|
172
|
+
|
173
|
+
set(target, key, value, receiver) {
|
174
|
+
|
175
|
+
console.log('YES!', { target, key, value, receiver }) //何らかの処理
|
176
|
+
|
177
|
+
target[ key ] = value
|
178
|
+
|
179
|
+
return true
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
const proxied = target => class extends target {
|
188
|
+
|
189
|
+
constructor(...arg) {
|
190
|
+
|
191
|
+
super(...arg)
|
192
|
+
|
193
|
+
const proxiedTarget = new Proxy(this, handler)
|
194
|
+
|
195
|
+
typeof this.handleEvent === 'function' && (this.handleEvent = this.handleEvent.bind(proxiedTarget))
|
196
|
+
|
197
|
+
return proxiedTarget
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
const ProxiedTarget = proxied(Target)
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
const element = document.body.appendChild(document.createElement('textarea'))
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
const proxiedTarget = new ProxiedTarget(element)
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
proxiedTarget.active = !proxiedTarget.active
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
proxiedTarget.active = !proxiedTarget.active
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
proxiedTarget.handleEvent({ type: 'focus' })
|
226
|
+
|
227
|
+
```
|