-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
500 lines (416 loc) · 9.4 KB
/
index.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
const year = new Date().getFullYear();
if (year % 4 === 0) {
if (year % 100 === 0) {
if (year % 400 === 0) {
console.log("${year}年はうるう年です");
} else {
console.log("${year}年はうるう年ではありません");
}
} else {
console.log("${year}年はうるう年です");
}
} else {
console.log("${year}年はうるう年ではありません");
}
const version = "ES6";
switch (version) {
case "ES5":
console.log("ECMAScript 5");
break;
case "ES6":
console.log("ECMAScript 2015");
break;
default:
console.log("しらないバージョンです");
break;
}
let total = 0;
for (let i = 0; i < 10; i++) {
total += i + 1;
}
function sum(numbers) {
let total = 0;
for (let i = 0; i < numbers.length; i++) {
total += numbers[i];
}
return total;
}
function sum(numbers) {
let total = 0;
numbers.forEach((num) => {
total += num;
});
return total;
}
sum([1, 2, 3, 4, 5]);
function isEven(num) {
return num % 2 === 0;
}
function isEvenInclueded(numbers) {
let isEvenInclueded = false;
for (let i = 0; i < numbers.length; i++) {
const num = numbers[i];
if (isEven(num)) {
isEvenInclueded = true;
break;
}
}
return isEvenInclueded;
}
const array = [1, 5, 10, 15, 20];
// function filterEven(numbers) {
// const results = [];
// for (let i = 0; i < numbers.length; i++) {
// const num = numbers[i];
// if (!isEven(num)) {
// continue;
// }
// results.push(num);
// }
// return results;
// }
// console.log(filterEven(array));
console.log(array.filter(isEven));
for (const value of array) {
console.log(value);
}
function sum(numbers) {
return numbers.reduce((total, num) => {
return total + num;
}, 0);
}
const obj1 = new Object();
console.log(obj1);
const languages = {
ja: "日本語",
en: "英語",
};
const { ja, en } = languages;
console.log(ja);
console.log(en);
("use strict");
const object = Object.freeze({ key: "value" });
object.key = "value";
const object1 = { key: undefined };
if (object1.hasOwnProperty("key")) {
console.log("'object1'は'key'プロパティを持っている");
}
const objee = {
a: {
b: "objのaプロパティのbプロパティ",
},
};
console.log(objee?.a?.b);
const shallowClone = (obj) => {
return Object.assign({}, obj);
};
const obj = { a: "a" };
const cloneObj = shallowClone(obj);
console.log(cloneObj);
console.log(obj === cloneObj);
const array1 = ["A", "B", "C", "D", "E"];
console.log(array1.slice(0, 5));
const array3 = [1, 2, 3];
array3.length = 0;
console.log(array3);
function myFunc() {
const argumentsArray = Array.from(arguments);
console.log(Array.isArray(argumentsArray));
argumentsArray.forEach((arg) => {
console.log(arg);
});
}
myFunc("a", "b", "c");
const pattern = /ES(\d+)/g;
const matchesIterator = "ES2015、ES2016、ES2017".matchAll(pattern);
for (const match of matchesIterator) {
console.log(
`match:"${match[0]}", capture1: ${match[1]}, index: ${match.index}, input: "${match.input}"`
);
}
function countOfCodePoints(str, codePoint) {
let count = 0;
for (const item of str) {
if (item === codePoint) {
count++;
}
}
return count;
}
console.log(countOfCodePoints("🐙🐙🙉 ", "🐙"));
const obj = {
method1: function () {
return this;
},
method2() {
return this;
},
};
console.log(obj.method1());
console.log(obj.method2());
const Prefixer = {
prefix: "pre",
prefixArray(strings) {
const that = this;
return strings.map(function (str) {
return that.prefix + "-" + str;
});
},
};
function outer() {
return () => {
return this;
};
}
const innerArrowFunction = outer();
console.log(innerArrowFunction());
class Myclass {}
const myClass = new Myclass();
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
//変数名にはキャメルケースを用いる
//コンストラクタ関数
const Point = function PointConstructor(x, y) {
this.x = x;
this.y = y;
};
const point = new Point(3, 4);
class ArrayLike {
constructor(items = []) {
this._items = items;
}
get items() {
return this._items;
}
get length() {
return this._items.length;
}
set length(newLength) {
const currentItemLength = this.items.length;
if (newLength < currentItemLength) {
this._items = this.items.slice(0, newLength);
} else if (newLength > currentItemLength) {
this._items = this.items.concat(new Array(newLength - currentItemLength));
}
}
}
const arrayLike = new ArrayLike([1, 2, 3, 4, 5]);
arrayLike.length = 2;
console.log(arrayLike.items.join(","));
arrayLike.length = 5;
console.log(arrayLike.items.join(","));
Object.getPrototypeOf(オブジェクト);
class Parent {
constructor(...args) {
console.log("#", ...args);
}
}
class Child extends Parent {
constructor(...args) {
super(...args);
console.log("#", ...args);
}
}
function assertPositiveNumber(num) {
if (num < 0) {
throw new Error(`${num} is not positive`);
}
}
try {
assertPositiveNumber(-1);
} catch (error) {
console.log(error instanceof Error);
console.log(error.message);
}
function reverseString(str) {
if (typeof str != "string") {
throw new TypeError(`$(str) is not a string`);
}
return Array.from(str).reverse().join("");
}
try {
reverseString(100);
} catch (error) {
console.log(error instanceof TypeError);
console.log(error.name);
console.log(error.message);
}
setTimeout(() => {
try {
throw new Error("エラー");
} catch (error) {
console.log("エラーをキャッチできる");
}
}, 10);
console.log("この行は実行されます");
function errorPromise(message) {
return new Promise((resolve, reject) => {
reject(new Error(message));
});
}
errorPromise("thenでエラーハンドリング").then(undefined, (error) => {
console.log(error.message);
});
errorPromise("catchでエラーハンドリング").catch((error) => {
console.log(erro.message);
});
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
reject(new Error("エラー"));
}, 16);
});
promise.then(
() => {
console.log("fullfilledとなった");
},
(error) => {
//この行はよびだされない
}
);
function asyncTask() {
return Math.random() > 0.5
? Promise.resolve("成功")
: Promise.reject(new Error("失敗"));
}
asyncTask()
.then(function onFulfilled(value) {
console.log(value);
})
.catch(function onRejected(error) {
console.log(error.message);
});
Promise.resolve(1)
.then((value) => {
console.log(value);
return value * 2;
})
.then((value) => {
console.log(value);
return value * 2;
})
.then((value) => {
console.log(value);
return value * 2;
});
function delay(timeoutMs) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(timeoutMs);
}, timeoutMs);
});
}
const racePromise = Promise.race([delay(1), delay(32), delay(64), delay(128)]);
async function doAsync() {
return "value";
}
doAsync().then((value) => {
console.log(value);
});
function dummyFetch(path) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (path.startsWith("/resource")) {
resolve({ body: `Response body of ${path}` });
} else {
reject(new Error("NOT FOUND"));
}
}, 1000 * Math.random());
});
}
async function fetchAB() {
const results = [];
const responseA = await dummyFetch("/resource/A");
results.push(responseA.body);
const responseB = await dummyFetch("/resource/B");
results.push(responseB.body);
return results;
}
fetchAB().then((results) => {
console.log(results);
});
async function fetchResources(resources) {
const results = [];
for (let i = 0; i < resources.length; i++) {
const resource = resources[i];
const response = await dummyFetch(resource);
results.push(response.body);
}
return results;
}
class ShoppingCart {
constructor() {
this.items = new Map();
}
//カートに商品を追加する
addItem(item) {
const count = this.items.get(item) ?? 0;
this.items.set(item, count + 1);
}
//カート内の合計金額を返す
getTotalPrice() {
return Array.from(this.items).reduce((total, [item, count]) => {
return total + item.price * count;
}, 0);
}
//カートの中身を文字列にして返す
toString() {
return Array.from(this.items)
.map(([item, count]) => {
return `${item.name}:${item.count}`;
})
.join(",");
}
}
const shoppingCart = new ShoppingCart();
const shopItems = [
{ name: "みかん", price: 100 },
{ name: "りんご", price: 200 },
];
// カートに商品を追加
shoppingCart.addItem(shopItems[0]);
shoppingCart.addItem(shopItems[0]);
shoppingCart.addItem(shopItems[1]);
// 合計金額を表示する
console.log(shoppingCart.getTotalPrice());
// カートの中身を表示
console.log(shoppingCart.toString());
const listenersMap = new WeakMap();
class EventEmitter {
addListener(listener) {
const listeners = listenersMap.get(this) ?? [];
const newListeners = listeners.concat(listener);
listenersMap.set(this, newListeners);
}
}
let eventEmitter = new EventEmitter();
eventEmitter.addListener(() => {
console.log("イベントが発火しました");
});
eventEmitter = null;
const set = new Set(["a", "b"]);
const results = [];
for (const value of set) {
results.push(value);
}
console.log(results);
const obj = { id: 1, name: "js-primer", bio: null };
const replacer = (key, value) => {
if (value === null) {
return undefined;
}
return value;
};
console.log(JSON.stringify(obj, replacer));
const now = new Date();
for (let i = 0; i < 5; i++) {
console.log(Math.random());
}
function getRandom(min, max) {
return Math.random() * (max - min) + min;
}
console.log(getRandom(1, 5));