-
Notifications
You must be signed in to change notification settings - Fork 0
/
14-1.html
121 lines (112 loc) · 3.63 KB
/
14-1.html
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" href="./public/favicon.ico" />
<meta http-equiv="cache-control" content="no-cache" />
<title></title>
<link rel="stylesheet" href=" https://necolas.github.io/normalize.css/8.0.1/normalize.css" />
<link rel="stylesheet" href="./hightlight/default.min.css" />
<link rel="stylesheet" href="./css/main.css" />
<link rel="stylesheet" href="./css/copybutton.css" />
<link rel="stylesheet" href="./css/hightlight.css" />
<script src="./hightlight/hightlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.11/clipboard.min.js"></script>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-BEVZJDBC7Z"></script>
<script src="./js/gtag.js"></script>
</head>
<body>
<header>
<nav>
<h1>
<span id="toggle-menu"></span>
<a href="index.html"></a>
</h1>
</nav>
</header>
<main>
<aside>
<nav></nav>
</aside>
<article>
<h2>新標準 ES2020 (ES11)</h2>
<h2 id="14-1">14-1 私有屬性和私有方法</h2>
<p>
在物件中不能被直接訪問的特性,就像女人的年齡和體重一樣是屬於私有資訊不能輕易被外界取得。
</p>
<pre><code class="language-js">
class Person{
// 公有屬性
name;
// 私有屬性
#age;
#weight;
// 建構子初始化
constructor(name,age,weight){
this.name = name
this.#age = age
this.#weight = weight
}
intro(){
console.log(girl.name)
console.log(girl.#age)
console.log(girl.#weight)
}
}
// 實體化
const girl = new Person('小姐姐','18','60kg')
console.log(girl) // Person {name: '小姐姐', #age: '18', #weight: '60kg'}
console.log(girl.name) // 小姐姐
console.log(girl.#age) // VM635:1 Uncaught Error: Cannot read private member #girl from an object whose class did not declare it
console.log(girl.#weight) // VM635:1 Uncaught Error: Cannot read private member #weight from an object whose class did not declare it
girl.intro()
// 小姐姐
// '18'
// '60kg'
</code></pre>
<p>同樣道理私有方法也是不能直接被使用。</p>
<pre><code class="language-js">
class ClassWithPrivateMethod {
#privateMethod() {
return 30;
}
}
const instance = new ClassWithPrivateMethod();
console.log(instance.privateMethod()); // Uncaught TypeError: instance.privateMethod is not a function
</code></pre>
<p>套一層公有方法就能有條件地得到私有的結果。</p>
<pre><code class="language-js">
class ClassWithPrivateMethod {
#age;
#privateMethod() {
return '臣妾永遠18歲';
}
publicMethod() {
this.#age = 30; // 實際年齡
return this.#privateMethod();
}
}
const instance = new ClassWithPrivateMethod();
console.log(instance.publicMethod()); // '臣妾永遠18歲'
</code></pre>
<p>資料來源:</p>
<p>
<a
href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields"
>
https://developer.mozilla.org/.../Private_class_fields
</a>
</p>
<p>
<a href="https://www.youtube.com/watch?v=rpW8xrIBMv4">
https://www.youtube.com/watch?v=rpW8xrIBMv4
</a>
</p>
</article>
</main>
</body>
<script type="module" src="./js/main.js"></script>
</html>