-
Notifications
You must be signed in to change notification settings - Fork 165
/
sub.js
207 lines (174 loc) · 5.12 KB
/
sub.js
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
export class Hello {
/** 非同期指定で呼び出します。 */
async say(message) {
{
log("💜ES2015 - テンプレート構文");
log(`${message}を出力しました`);
}
{
log("💛ES2016 - べき乗の検証");
log(3 ** 2); // 9
log(2 ** 8); // 256
log(2 ** 10); // 1024
}
{
log("💛ES2016 - Array.include の検証");
const arr = ["hoge", "fuga", "piyo"];
log(arr.includes("hoge")); // true
log(arr.includes("moja")); // false
}
{
log("💚ES2017 - async, await の検証");
const numList = [1, 2, 3];
const numList2 = await Promise.all(numList.map((id) => id * 2));
log(numList2); // [2, 4, 6]
}
{
log("💚ES2017 - String.prototype.padEnd() の検証");
const str = "moja";
log(str.padEnd(10, "hoge")); // mojahogeho
log(str.padEnd(5, "piyo")); // mojap
}
{
log("💙ES2018 - Asynchronous Iteration");
// ES2018 : async, await の検証
const numList = [1, 2, 3];
for (let i of numList) {
await new Promise((resolve) => {
log(i);
resolve();
});
}
numList.forEach(async (i) => {
await new Promise((resolve) => {
log(i);
resolve();
});
});
}
{
log("💙ES2018 - Rest/Spread Properties");
restParam(1, 2, 3, 4, 5);
function restParam(p1, p2, ...p3) {
log(p1); // 1
log(p2); // 2
log(p3); // [3, 4, 5]
}
}
{
log("💙ES2018 - Regular Expression Named Capture Groups");
const reDate = /([0-9]{4})-([0-9]{2})-([0-9]{2})/;
const match = reDate.exec("2018-04-30");
const year = match[1]; // 2018
const month = match[2]; // 04
const day = match[3]; // 30
log(year); // 2018
log(month); // 04
log(day); // 30
}
try {
const instance = new MyClass();
instance.show();
log("💗️ES2019 - flatの検証");
log([[1, 2], 3, 4].flat()); // 結果:[1, 2, 3, 4]
log(
["牛", "豚", "マグロ"].flatMap((food) => [food, `${food}丼`])
); // 結果:["牛", "牛丼", "豚", "豚丼", "マグロ", "マグロ丼"]
log("💗️ES2019 - fromEntriesの検証");
log(
Object.fromEntries([
["id", 16],
["name", "鈴木"],
])
);
try {
throw new Error("🙅");
} catch {
// (error)の指定は必須
console.warn("💗️ES2019 - エラーの省略");
}
} catch (error) {
// IE11向けにはArray#flat()はポリフィルでも利用できない
// https://github.com/babel/babel/issues/9749#issuecomment-475686062
// core-js@3を利用する方法も案としてあげられるが、別の問題がつきまとい現実的ではない
log("[NG] ES2019 - flatの検証は利用できませんでした");
}
{
const object = {};
const fuga = object.hoge?.fuga;
log("💗️ES2020 - オプショナルチェイニング", fuga);
}
{
const num1 = 1_000_000_000; // 10億(1,000,000,000)
log("😊ES2021 - 数値の _ 記号", num1);
}
{
const human = { name: "鈴木" };
human.name ??= "佐藤";
// human.name は nullish ではないので、何も代入されない
human.address ??= "港区";
// human.address は nullish なので、「港区」が代入される
// 結果: {name: "鈴木", address: "港区"}
log("😊ES2021 - ??=演算子", human);
}
{
const instance = new MyClass2022();
log(instance.show());
log(MyClass2022.myStaticField);
}
{
const element = ["a", "b", "c", "d", "e"].at(-1); // e
log("🍋ES2022 - .at. last element is " + element);
}
{
const object = { foo: false };
log(
"🍋ES2022 - .Accessible Object.prototype.hasOwnProperty, " +
object.hasOwnProperty("foo")
);
}
{
const result = /EF(GH)(I)J/d.exec("ABCDEFGHIJK");
log("🍋ES2022 - RegExp Match Indices");
console.table(result.indices);
}
{
const list1 = [1, 2, 3];
const list2 = list1.toReversed(); // 逆転
log("🍊ES2023 - toReversed()", {list1, list2});
}
{
const list1 = [3, 1, 2];
const list2 = list1.toSorted(); // ソート
log("🍊ES2023 - toSorted()", {list1, list2});
}
}
}
class MyClass {
// hoge = '💗ES2019 - メンバーフィールドの検証';
constructor() {}
show() {
// log(this.hoge);
}
}
class MyClass2022 {
static myStaticField = "🍋ES2022 - staticフィールドの検査法";
#privateFiled = "🍋ES2022 - プライベートフィールドの検証";
constructor() {}
show() {
log(this.#privateFiled);
}
#show() {
log(this.#privateFiled);
}
}
function log(arg){
const str = [...arguments].map((arg) => {
if (typeof arg === "object") {
return JSON.stringify(arg);
}
return arg;
}).join(", ");
const element = document.querySelector("#consoleArea");
element.innerHTML = element.innerHTML + "\n" + str;
}