質問編集履歴

1

追加

2019/09/04 07:20

投稿

shoshaashin
shoshaashin

スコア4

test CHANGED
File without changes
test CHANGED
@@ -37,3 +37,111 @@
37
37
 
38
38
 
39
39
  回答のほど、よろしくお願いいたします。
40
+
41
+
42
+
43
+ ### 追加
44
+
45
+ tetsunosukeさんが教えてくださった参考ページの例について自分でコードを動かして
46
+
47
+ 確認していたのですが、以下の点が分かりました。
48
+
49
+ 間違っている部分や足りない部分がありましたらご指摘いただけると幸いです。
50
+
51
+
52
+
53
+ [分かった点]
54
+
55
+ ・staticメソッド内でのthisはclass自身を指すため、staticメソッドから他のstaticメソッドへは this.staticメソッド で呼び出せる
56
+
57
+ ・クラス内でクラスを明示的に呼び出すことももちろん出来る(例の場合だとStaticMethodClass.staticメソッドのように)
58
+
59
+ ・インスタンスメソッド内のthisはインスタンスを指すため、staticメソッドにアクセスする場合はconstructorプロパティで生成元を辿って参照しなければならない
60
+
61
+
62
+
63
+ よろしくお願いいたします。
64
+
65
+
66
+
67
+ ```javascript
68
+
69
+ class StaticMethodCall{
70
+
71
+
72
+
73
+
74
+
75
+ constructor(){
76
+
77
+ this.className = "StaticMethodCall Class.";
78
+
79
+
80
+
81
+ console.log(StaticMethodCall.staticMethod());
82
+
83
+ // 'static method has been called'
84
+
85
+
86
+
87
+ console.log(this.constructor.staticMethod()); //インスタンス生成元をconstructorを辿っている
88
+
89
+ // 'static method has been called'
90
+
91
+ }
92
+
93
+
94
+
95
+ static staticMethod(){
96
+
97
+ return 'static method has been called.';
98
+
99
+ }
100
+
101
+
102
+
103
+ isThis(){
104
+
105
+ //staticメソッドでない場合、thisはインスタンスを指す?
106
+
107
+ return `
108
+
109
+ this: ${this}
110
+
111
+ this === StaticMethodCall: ${this === StaticMethodCall} false
112
+
113
+ this.className = ${this.className} //StaticMethodCall Class.
114
+
115
+ `;
116
+
117
+ }
118
+
119
+
120
+
121
+ static isThis(){
122
+
123
+ //staticメソッド内でのthisはクラス自体を指す
124
+
125
+ return `
126
+
127
+ this: ${this}
128
+
129
+ this === StaticMethodCall: ${this === StaticMethodCall} true
130
+
131
+ this.className = ${this.className} //undefined
132
+
133
+ `;
134
+
135
+ }
136
+
137
+ }
138
+
139
+
140
+
141
+ let sm = new StaticMethodCall();
142
+
143
+ console.log(sm.isThis());
144
+
145
+ console.log(StaticMethodCall.isThis());
146
+
147
+ ```