質問編集履歴
2
デモコード修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -3,13 +3,17 @@
|
|
3
3
|
//メソッド
|
4
4
|
|
5
5
|
class A {
|
6
|
-
static
|
6
|
+
static _bar = 'bar'
|
7
|
+
static #baz = 'baz'
|
7
8
|
static getFoo() {
|
8
9
|
return 'foo'
|
9
10
|
}
|
10
11
|
static getBar() {
|
11
|
-
return this.
|
12
|
+
return this._bar
|
12
13
|
}
|
14
|
+
static getBaz() {
|
15
|
+
return this.#baz
|
16
|
+
}
|
13
17
|
}
|
14
18
|
|
15
19
|
class B extends A {
|
@@ -20,20 +24,26 @@
|
|
20
24
|
|
21
25
|
console.log('A.getFoo()', A.getFoo()) //OK
|
22
26
|
console.log('A.getBar()', A.getBar()) //OK
|
27
|
+
console.log('A.getBaz()', A.getBaz()) //OK
|
23
28
|
console.log('B.getFoo()', B.getFoo()) //OK
|
29
|
+
console.log('B.getBar()', B.getBar()) //OK
|
24
|
-
console.log('B.
|
30
|
+
console.log('B.getBaz()', B.getBaz()) //NG //Uncaught TypeError: Cannot read private member #baz from an object whose class did not declare it
|
25
31
|
```
|
26
32
|
```javascript
|
27
33
|
//ゲッター
|
28
34
|
|
29
35
|
class A {
|
30
|
-
static
|
36
|
+
static _bar = 'bar'
|
37
|
+
static #baz = 'baz'
|
31
38
|
static get foo() {
|
32
39
|
return 'foo'
|
33
40
|
}
|
34
41
|
static get bar() {
|
35
|
-
return this.
|
42
|
+
return this._bar
|
36
43
|
}
|
44
|
+
static get baz() {
|
45
|
+
return this.#baz
|
46
|
+
}
|
37
47
|
}
|
38
48
|
|
39
49
|
class B extends A {
|
@@ -44,11 +54,13 @@
|
|
44
54
|
|
45
55
|
console.log('A.foo', A.foo) //OK
|
46
56
|
console.log('A.bar', A.bar) //OK
|
57
|
+
console.log('A.baz', A.baz) //OK
|
47
58
|
console.log('B.foo', B.foo) //OK
|
59
|
+
console.log('B.bar', B.bar) //OK
|
48
|
-
console.log('B.
|
60
|
+
console.log('B.baz', B.baz) //NG //Uncaught TypeError: Cannot read private member #baz from an object whose class did not declare it
|
49
61
|
```
|
50
|
-
「`#
|
62
|
+
「`#baz`」が宣言されていないので呼べません!的なことを言われますが普通はどうするものですか?
|
51
63
|
|
52
|
-
「`B.
|
64
|
+
「`B.getBaz()`」も「`B.baz`」もエラーなく呼びたいです。
|
53
65
|
|
54
66
|
Chrome87.0.4280.88だけで確認しています。
|
1
デモコード追加
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
継承すると、静的プライベートフィールドを参照している静的メソッドで
|
1
|
+
継承すると、静的プライベートフィールドを参照している静的メソッドでエラー
|
body
CHANGED
@@ -1,7 +1,33 @@
|
|
1
1
|
# 継承すると、静的プライベートフィールドを参照している静的メソッドでタイプエラーが出ます
|
2
2
|
```javascript
|
3
|
+
//メソッド
|
4
|
+
|
3
5
|
class A {
|
4
6
|
static #bar = 'bar'
|
7
|
+
static getFoo() {
|
8
|
+
return 'foo'
|
9
|
+
}
|
10
|
+
static getBar() {
|
11
|
+
return this.#bar
|
12
|
+
}
|
13
|
+
}
|
14
|
+
|
15
|
+
class B extends A {
|
16
|
+
constructor() {
|
17
|
+
super()
|
18
|
+
}
|
19
|
+
}
|
20
|
+
|
21
|
+
console.log('A.getFoo()', A.getFoo()) //OK
|
22
|
+
console.log('A.getBar()', A.getBar()) //OK
|
23
|
+
console.log('B.getFoo()', B.getFoo()) //OK
|
24
|
+
console.log('B.getBar()', B.getBar()) //NG //Uncaught TypeError: Cannot read private member #bar from an object whose class did not declare it
|
25
|
+
```
|
26
|
+
```javascript
|
27
|
+
//ゲッター
|
28
|
+
|
29
|
+
class A {
|
30
|
+
static #bar = 'bar'
|
5
31
|
static get foo() {
|
6
32
|
return 'foo'
|
7
33
|
}
|
@@ -21,8 +47,8 @@
|
|
21
47
|
console.log('B.foo', B.foo) //OK
|
22
48
|
console.log('B.bar', B.bar) //NG //Uncaught TypeError: Cannot read private member #bar from an object whose class did not declare it
|
23
49
|
```
|
24
|
-
「#bar」が宣言されていないので呼べません!的なことを言われますが普通はどうするものですか?
|
50
|
+
「`#bar`」が宣言されていないので呼べません!的なことを言われますが普通はどうするものですか?
|
25
51
|
|
26
|
-
「B.bar」もエラーなく呼びたいです。
|
52
|
+
「`B.getBar()`」も「`B.bar`」もエラーなく呼びたいです。
|
27
53
|
|
28
54
|
Chrome87.0.4280.88だけで確認しています。
|