當你需要驗證資料時,類別特別有用。以這個 Box
類別為例,它不應該能超出滑桿允許的最大值來持續增大,但這正是發生的情況。
我們可以通過將 width
和 height
替換為 *getter* 和 *setter*(也稱為 *accessor*)來修復此問題。首先,將它們轉換為私有屬性
App
class Box {
#width = $state(0);
#height = $state(0);
area = $derived(this.#width * this.#height);
constructor(width, height) {
this.#width = width;
this.#height = height;
}
// ...
}
然後,建立一些 getter 和 setter
App
class Box {
// ...
get width() {
return this.#width;
}
get height() {
return this.#height;
}
set width(value) {
this.#width = value;
}
set height(value) {
this.#height = value;
}
embiggen(amount) {
this.width += amount;
this.height += amount;
}
}
最後,將驗證新增到 setter
App
set width(value) {
this.#width = Math.max(0, Math.min(MAX_SIZE, value));
}
set height(value) {
this.#height = Math.max(0, Math.min(MAX_SIZE, value));
}
現在,無論是透過範圍輸入上的 bind:value
,還是 embiggen
方法,都無法將方塊尺寸增大到安全限制之外,無論你多用力按按鈕都一樣。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<script>
const MAX_SIZE = 200;
class Box {
width = $state(0);
height = $state(0);
area = $derived(this.width * this.height);
constructor(width, height) {
this.width = width;
this.height = height;
}
embiggen(amount) {
this.width += amount;
this.height += amount;
}
}
const box = new Box(100, 100);
</script>
<label>
<input type="range" bind:value={box.width} min={0} max={MAX_SIZE} />
{box.width}
</label>
<label>
<input type="range" bind:value={box.height} min={0} max={MAX_SIZE} />
{box.height}
</label>
<button onclick={() => box.embiggen(10)}>embiggen</button>
<hr>
<div
class="box"
style:width="{box.width}px"
style:height="{box.height}px"
>
{box.area}
</div>
<style>
label {
display: flex;
align-items: center;
}
hr {
margin: 1em 0;
border: none;
border-bottom: 1px solid #888;
}
.box {
background: radial-gradient(at 25% 25%, hsl(15 100 60), hsl(15 100 50)) ;
border-radius: 2px;
filter: drop-shadow(0 0 10px hsl(15 100 50 / 0.3));
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
}
</style>