回答編集履歴

1

class 構文 (ECMAScript 6)

2017/12/25 09:26

投稿

think49
think49

スコア18164

test CHANGED
@@ -1,3 +1,7 @@
1
+ ### コンストラクタ (ECMAScript 3)
2
+
3
+
4
+
1
5
  紹介されている例をコードに表すと、次のように書けると思います。
2
6
 
3
7
 
@@ -84,4 +88,54 @@
84
88
 
85
89
 
86
90
 
91
+ ### class 構文 (ECMAScript 6)
92
+
93
+
94
+
95
+ 上述のコードを ECMAScript 6 規定の class 構文を使って書き直すと、次のようになります。
96
+
97
+
98
+
99
+ ```JavaScript
100
+
101
+ 'use strict';
102
+
103
+ class Cat {
104
+
105
+ constructor (name, age, color, sex, thisFunctions) {
106
+
107
+ this.name = name;
108
+
109
+ this.age = age;
110
+
111
+ this.color = color;
112
+
113
+ this.sex = sex;
114
+
115
+
116
+
117
+ for (var i = 0, len = thisFunctions.length, fn; i < len; ++i) {
118
+
119
+ fn = thisFunctions[i];
120
+
121
+ this[fn[0]] = fn[1];
122
+
123
+ }
124
+
125
+ }
126
+
127
+
128
+
129
+ slappingHands () {
130
+
131
+ console.log('slapping hands');
132
+
133
+ }
134
+
135
+ }
136
+
137
+ ```
138
+
139
+
140
+
87
141
  Re: bellsmarket さん