ð JavaScript 第7ç
æ¬æžãåŠç¿ããèæ¯ -2min-
JSã¯Progateããã®åŸã¹ã¯ãŒã«ã§ã«ãªãã¥ã©ã ã§å°ã觊ã£ãããšãããçšåºŠã ã£ãã
ãã®ç¶æ
ã§èå³æ¬äœã§ReactãTypeScriptãåŠç¿ããã
ãã®éã«åºæ¬çãªæ§æã®ç解ãæµ
ãããšãåå ã§ã³ãŒããªãŒãã£ã³ã°ã«æéãããããšãããã£ãã
çç±ãæ¢ããšãJSïŒããã³ãåŽïŒã®æè¡çãªã¢ããããŒãã®é »åºŠãæ©ãããšããã¬ã³ãã®å
¥ãæ¿ãããæ©ãããšããé¢æ°ã®å®çŸ©ã®ä»æ¹ãªã©ãè€æ°ããããŸãèšäºãæžç±ãªã©ããã®åœ±é¿ãåããŠãããæçš¿ææãçºå£²æ¥æã«ãã£ãŠã¯æ§æãéãã
ãªã®ã§ãæ¯åæ€çŽ¢ããã³ã¹ããããããããã³ãåŽã§äœ¿çšãããŠããã©ã€ãã©ãªã¯ã»ãšãã©ãJSã䜿çšããŠããããšããæ¬æžã§æ¬æ Œçã«åŠç¿ããããšã«ããã
æ¬Scrapã®çšé
- å人ã®ã¢ãŠããããç®ç
- å匷äŒãã¬ãŒã³çšè³æ
æžç±å ïŒèšäºå ïŒã§äœ¿çšããã³ãŒã
åèæç®
ã¹ã±ãžã¥ãŒã«
- èæ¯ 2min
- 1ç« JSæŠèŠ 10min
- 2ç« åå¥æ§é 3min
- 3ç« åãå€ãå€æ° 5min
- 4ç« åŒãšæŒç®å 10min
- 5ç« æ 10min
- 6ç« ãªããžã§ã¯ã 5min
- 7ç« é å 10min
- 8ç« é¢æ° 15min
- 9ç« ã¯ã©ã¹è§Šã 5min
- SPAãšã¯ãªã«ã 10min
- TypeScriptãšã¯ãªã«ã 5min
åèš 1.5h
1ç« JavaScriptæŠèŠ -10min-
JSã£ãŠã©ãã§äœ¿ãããŠãã
- ãã©ãŠã¶API
ãŠã§ããã©ãŠã¶ãŒã«çµã¿èŸŒãŸããŠããŠãã³ã³ãã¥ãŒã¿ãŒãåãå·»ãç°å¢ããããŒã¿ãåãåºããããè€éã§äŸ¿å©ãªããšãããŠãããããã
- ãµãŒãããŒãã£API
ãã©ãŠã¶ã«çµã¿èŸŒãŸããŠããªãã®ã§ãæ®éã¯ãŠã§ãäžã®ã©ããããã³ãŒããšæ å ±ãæ¢ããªããã°ãªããªãã
JavaScriptãèªãäžã§ã®ããããåæ
JavaScriptã¯äœãããã
ãŠã§ãããŒãžããã©ãŠã¶ãŒã§èŠããšããå®è¡ç°å¢ (ãã©ãŠã¶ãŒã®ã¿ã) ã®äžã§ãã³ãŒã (HTMLãCSS ãã㊠JavaScript) ãå®è¡ãããŸããããã¯çŽ æ (ã³ãŒã) ãå å·¥ããŠè£œå (ãŠã§ãããŒãž) ãåºåããå·¥å Žã®ãããªãã®ã§ãã
åŒçš : MDN web docs
ããã«è©³ããHTMLâCSSâJSã®æµããç¥ãã«ã¯...
ã€ã³ã¿ãŒããªã¿ãŒãšã³ã³ãã€ã«ã³ãŒã
ã€ã³ã¿ãŒããªã¿ãŒã§ã¯ãã³ãŒããäžããäžã«å®è¡ãããŠã³ãŒãã®å®è¡çµæãããã«è¿ã£ãŠããŸãããã©ãŠã¶ãŒãå®è¡ããåã«ã³ãŒããäœããã®åœ¢ã«å€æããå¿ èŠã¯ãããŸããã
åŒçš : MDN web docs
ã³ã³ãã€ã«èšèªã¯ã³ã³ãã¥ãŒã¿ãŒã§å®è¡ããåã«ä»ã®åœ¢åŒã«å€æ (ã³ã³ãã€ã«) ããªããã°ãªããŸããã
åŒçš : MDN web docs
ããããTypeScript
ã䜿ãããŠããåå ã ã£ãããã
JavaScript
ã¯ãäºåã§ã¯ãªãå®è¡æã«ã³ã³ãã€ã«ãããããã«ãã€ã³ã¿ãŒããªã¿ãŒèšèªãšèããããŠããŸãã
åŒçš : MDN web docs
ãµãŒããŒãµã€ãã³ãŒããšã¯ã©ã€ã¢ã³ãã³ãŒã
ã¯ã©ã€ã¢ã³ããµã€ãã³ãŒãã¯ãŠãŒã¶ãŒã®ã³ã³ãã¥ãŒã¿ãŒäžã§å®è¡ãããã³ãŒãã§ãããŠã§ãããŒãžãèŠãŠãããšããããŒãžã®ã¯ã©ã€ã¢ã³ããµã€ãã³ãŒããããŠã³ããŒããããŠããã©ãŠã¶ãŒã§å®è¡ãããŠè¡šç€ºãããŸãããã® JavaScript ã¢ãžã¥ãŒã«ã®ããšãæ瀺çã«ã¯ã©ã€ã¢ã³ããµã€ã JavaScript ãšèšããŸãã
åŒçš : MDN web docs
ãµãŒããŒãµã€ãã³ãŒãã¯ãµãŒããŒäžã§å®è¡ãããçµæããã©ãŠã¶ãŒã«ããŠã³ããŒããããŠè¡šç€ºãããŸãããŠã§ãã§äººæ°ã®ãããµãŒããŒãµã€ãã®èšèªã¯ãPHPãPythonãRuby ã ASP.NET ãªã©ããã㊠JavaScript ã§ãïŒ JavaScript ã¯ãµãŒããŒãµã€ãã®èšèªãšããŠã䜿ãããŸãã人æ°ã®ãã Node.js ç°å¢ããã®äŸã§ãã
åŒçš : MDN web docs
å®è¡ç°å¢
- ãã©ãŠã¶ã®ã³ã³ãœãŒã«ïŒå人çã«ã¯
Chrome
ããFirefox
ã®æ¹ããã£ãããïŒ -
Node.js
ãå ¥ã£ãŠããå Žåã¯ãã¿ãŒããã«ã§$ node
ã§JavaScript
ã®ã³ã³ãœãŒã«ãéã
2ç« åå¥æ§é -3min-
å ·äœçã«ä»¥äžã®ãããªãã®ã®ã«ãŒã«ãæã
- 倧æåå°æåã®åºå¥ã空çœãæ¹è¡
- ã³ã¡ã³ã
- ãªãã©ã«
- èå¥åãšäºçŽèª
Unicode
Unicodeãšã¯ãã³ã³ãã¥ãŒã¿ã䜿ãããæåããšãæåã«å²ãåœãŠãçªå·ãã®å¯Ÿå¿è¡šãã®ã²ãšã€ãã³ã³ãã¥ãŒã¿ã¯2é²æ°ãªã®ã§ãæåã2é²æ°ã§è¡šããã®
- çç¥å¯èœãªã»ãã³ãã³
詳ããç¥ã£ãŠãããã»ããããããš
- ãªãã©ã«ãšã¯ã
JS
ã§ããã°ã©ã ã«çŽæ¥èšè¿°ããããŒã¿å€ã®ããš
// 以äžã®ãããªãã®
12
1.2
'Hello'
true
- èå¥åãšäºçŽèª
èå¥åãšã¯ãååã®ããšïŒå®äœãå€æ°ãããããã£ãé¢æ°ãã¯ã©ã¹ãªã©ïŒ
äºçŽèªãšã¯ãJSã«ãããããèšèªã®äžéšãšããŠäœ¿çšãããŠãããã®ïŒ
if
ãfor
ãªã©ïŒ
- ã»ãã³ãã³ã¯æ瀺çã«ç€ºãã»ãããã
return
true;
// äžèšã¯ return; true; ãšãããµãã«è§£éãããŠããŸã
let y = x + f
(a+b).toString()
// äžèšãåã let y = x + f(a+b).toString() ãšè§£éããã
// æ§æã«ãã£ãŠã¯ãäºæãã¬ãšã©ãŒã«ãªãã
3ç« åãå€ãå€æ° -5min-
åããã¥ããèšèã®å®çŸ©ããã£ããããŠãã
- åºæ¬åïŒæ°å€ãæååãè«çå€ãnullãundefinedïŒ
- ãªããžã§ã¯ãåïŒäžèšä»¥å€ã®ãã®ããããã£ã®éåïŒ
ããšãã説æãããé åãé¢æ°ããªããžã§ã¯ãã®1çš®ã§ãã
äžå€ãªåºæ¬åå€ãšå¯å€ãªãªããžã§ã¯ãåç §
- åºæ¬åïŒæ°å€ãæååãè«çå€ãnullãundefinedïŒ
- ãªããžã§ã¯ãåïŒäžèšä»¥å€ã®ãã®ããããã£ã®éåïŒ
äžèšã®2ã€ã®éããšããŠãåºæ¬åã¯äžå€ã§ããããªããžã§ã¯ã/é åã¯å¯å€ã§ããã
// åºæ¬åã¯äžå€ã§ãã
// æååãå€æŽããããã«èŠããããæååãã®ãã®ãå€æŽããã®ã§ã¯ãªããæ°ããªæååãè¿ã
let s = "hello";
s.toUpperCase(); // "HELLO" ãšå€ãè¿ããã
s // => "hello" : å
ã®æååã¯å€æŽãããªã
// ãªããžã§ã¯ãã»é
åã¯å¯å€ã§ããã
let o = { x: 1 };
o.x = 2 // 2
// ããããã£ã®å€ãå€æŽããŠããªããžã§ã¯ããæŽæ°ãã
// é
åãå¯å€
// ãã ãã2ã€ã®ãªããžã§ã¯ãã¯åãããããã£ãæã¡ãå€ãåãã ã£ããšããŠãç°ãªããªããžã§ã¯ããšããŠå€æããã;
let a = [1, 2, 3]
o === p // false
// å¥ã
ã®ç©ºã®é
å
let a = [], b = [];
a === b // false
å šäœå
å€ | æåå | æ°å€ | è«çå€ |
---|---|---|---|
undefined | "undefined" | 0 | false |
null | "null" | 0 | false |
true | "true" | 1 | |
false | "false" | 0 | |
""(空æåå) | 0 | false | |
"1.2"(æ°å€æåå) | 0 | false | |
"one" | NaN | true | |
0 | "0" | false | |
-0 | "0" | false | |
1(ãŒãã§ã¯ãªãæéå€) | "1" | true | |
Infinity | "Infinity" | true | |
-Infinity | "-Infinity" | true | |
NaN | "NaN" | false | |
{}(ä»»æã®ãªããžã§ã¯ã) | (å¿ èŠã«å¿ããŠå€æŽå¯èœ) | (å¿ èŠã«å¿ããŠå€æŽå¯èœ) | true |
[] (空ã®é
å) |
"" | 0 | true |
[9] (èŠçŽ ãæ°å€1ã€ã®é
å) |
"9" | 9 | true |
['a'] (ãã以å€ã®é
å) |
joinã¡ãœããã䜿çšãã | NaN | true |
function(){}(ä»»æã®é¢æ°) | (å¿ èŠã«å¿ããŠå€æŽå¯èœ) | NaN | true |
NaNãšã¯Not-a-Numberã®ç¥ã§ãJavaScriptã«ãããéæ°ïŒæ°åã§ã¯ãªããã®ïŒãè¡šããç¹å¥ãªå€ã§ãã äžè¬çã«ã¯ãæ°å€ãæåŸ ããŠãããã©å®ã¯æ°å€ã§ã¯ãªãã£ãå Žåã®çµæãšããŠäœ¿ãããããšãå€ã
JSã®æé»çãªå€æ
JavaScriptã§åå€æã¯ãæé»çã«å€æããŠãããã
ããŸãå€æãããªããšããæ瀺çã«ãããå Žåã¯ãString()``Number()
ãªã©ã䜿çšããã
調ã¹ãŠã¿ããšãçµæ§ããã©ãããããããªæã«TypeScript
䜿çšãããšããããããããŸãã³ã³ãã€ã«åã«ãšã©ãŒããããã
letãšvarãšconstã®å šäœå
ES2015(ES6)
ãåºããŸã§ã¯ãvar
ãå€æ°å®£èšããå¯äžã®æ¹æ³
ES2015(ES6)
以éã«let
ãšconst
ãç»å Žããããã以éã¯åºæ¬çã«ã¯ãlet
ãšconst
ã䜿çšãããŠããã
var | let | const | |
---|---|---|---|
åä»£å ¥ | â | â | Ã(åæå€ãå¿ èŠ) |
åå®£èš | â | à | à |
ã¹ã³ãŒã | é¢æ°ã¹ã³ãŒã | ãããã¯ã¹ã³ãŒã | ãããã¯ã¹ã³ãŒã |
letãšvar
let x = 1;
if (x === 1) {
let x = 2;
console.log(x);
// expected output: 2
}
console.log(x);
// expected output: 1
var x = 1;
if (x === 1) {
var x = 2;
console.log(x);
// expected output: 2
}
console.log(x);
// expected output: 2
const
JSã§å€æ°å®£èšã®æã«äœ¿çšã§ããå®æ°
var japan = true;
japan = false;
console.log(japan); // false
// äžèšã ãšãå代å
¥å¯èœãªå€æ°ã
// constãç»å Žã§å®æ°ã§å®£èšã§ãã(å€ãäžå€ã§ããããšã蚌æã§ãã)ã
const japan = true;
japan = false;
// Uncaught TypeError: Assignment to constant variable.
// ãšã©ãŒã«ãªã
4ç« åŒãšæŒç®å -15min-
èšèã®å®çŸ©
é¢æ°å®çŸ©åŒ
let square = function(x) {
return x * x;
};
// ES6以éã§ã¯ã¢ããŒé¢æ°ããã䜿çšãã
ããããã£ã®ã¢ã¯ã»ã¹
let o = {x:1, y: {z: 3}};
o.x // 1
o.y.z // 3
â»æ¡ä»¶ä»ãããããã£
äžèšã®ãããªãšãã§// TypeError
ãåé¿ããããšããªã©ã«äœ¿çšã§ãã
let a = { b: null };
a.b?.c.d // undefined
null
ãundefined
ãªã©ã«// TypeError
ãåé¿ããŠãundefined
ãåããŠããã
â»æ¡ä»¶ä»ãåŒã³åºã
äžèšã®ãããªæ¡ä»¶ä»ãããããã£ã«äŒŒãŠããåŠçãé¢æ°ã®åŒã³åºãã§ã䜿çšã§ããã
f(0); // éåžžã®é¢æ°åŒã³åºã
// `null`ã`undefined`ã®ãšãã« `// TypeError`ãåºã
f?.(); // ãã®ããã«åŒã³åºãã°ã`null`ã`undefined`ã®ãšãã«`undefined`ãè¿ã
ãã䜿ãæŒç®å
// çå€æŒç®åïŒ`==`ïŒ
// ãªãã©ã³ããçããå Žåã« true ãè¿ã
let test = 3;
3 == test // true
"3" == test // true
3 == '3' // true
// åå€æŒç®å(`===`)
// ãªãã©ã³ããçããããã€åãåã§ããå Žåã« true ãè¿ã
let test = 3;
3 === test // true
3 == '3' // false
// inæŒç®å
// 巊蟺ã®å€ãå³èŸºã®ãªããžã§ã¯ãã®ããããã£åã«ããã°`true`ãè¿ã
let point = {x: 1, y:1}
"x" in point // true
// instanceofæŒç®å
// 巊蟺ã®ãªããžã§ã¯ããå³èŸºã®ã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ãªã`true`ãè¿ã
let d = new Date();
d instanceof Date // true
// &&
// äž¡æ¹`true`ãªã`true`ãè¿ã
// ||
// äžæ¹ãŸãã¯äž¡æ¹`true`ãªã`true`ãè¿ã
// !
// è«çå€ãå転ãã
// äžé
æŒç®å
// ããèŠããã€
x > 0 ? x: -x // x
// ãã¡ãŒã¹ãããã¡ã€ã³ãæŒç®å
// 巊蟺ã`null`ã`undefined`ã®ãšãã¯å·ŠèŸºãè¿ãã`null`ã`undefined`ã®ãšãã¯å³èŸºãè¿ã
let options = { timeout: 0 }
options.timeout ?? 1000 // 0
options.n ?? 1000 // 1000
// typeofæŒç®å
ããŒã¿åãè¡šãæååãè¿ã
// P102äžèŠ§è¡šåèã«
// deleteæŒç®å
// æå®ãããªããžã§ã¯ããããããã£ãåé€ããããã ããé·ãã¯å€ãããªãã®ã§æ³šæãå¿
èŠ
let a = [1, 2, 3];
delete a[2];
2in a // false
a.length // 3 é·ãã¯å€æŽãããªã
// å€ã`undefined`ã«å€æŽãããã ã
// ã«ã³ãæŒç®å
i = 0, j = 1, k = 2;
// äžèšãšåã
i = 0; j = 1; k = 2;
5ç« æ -10min-
å šäœå
æ | æå³ |
---|---|
const | 1ã€ä»¥äžã®å®æ°ã宣èšãåæåãã |
let | 1ã€ä»¥äžã®å€æ°ã宣èšãåæåããããã®å€æ°ã¯ãããã¯ã¹ã³ãŒããæ〠|
var | 1ã€ä»¥äžã®å€æ°ã宣èšãåæåãããã°ããŒãã«ãªå€æ°ãšããŠæ±ãïŒå€ãæ§æïŒ |
while | åºæ¬çãªã«ãŒãæãäœæãã |
do/while |
while ã«ãŒãã®ä»£æ¿è¡šèš |
for | ç°¡åã«äœ¿ããã«ãŒãæ |
for/in | ãªããžã§ã¯ãã®ããããã£ã調ã¹ã |
for/of | é åãªã©ã®å埩å¯èœãªããžã§ã¯ãã®å€ã調ã¹ã |
for/await | éåæã€ãã¬ãŒã¿ã®å€ãå埩ãã |
switch |
case ãŸãã¯default: ã§ã©ãã«ãã€ããæã«åå²ãã |
case |
swicthæ å
ã®æã«ã©ãã«ãã€ãã |
default |
switchæ å
ã®ããã©ã«ãã®æã«ã©ãã«ãã€ãã |
break | æãå
åŽã®ã«ãŒããswitchæ ãã©ãã«ã§æå®ãããæãçµäºãã |
continue | æãå åŽã®ã«ãŒãæããŸãã¯ã©ãã«ã§æå®ãããã«ãŒãæã次ã«é²ãã |
ã©ãã«æ |
break ãcountinue ã§äœ¿ããããã«æã«ã©ãã«ãã€ãã |
return | é¢æ°ããå€ãè¿ã |
yield | å埩åŠççšã«å€ãçæããããžã§ãã¬ãŒã¿é¢æ°å ã§ã®ã¿å©çšã§ãã |
if/else | æ¡ä»¶ã«ãã£ãŠå®è¡ããæãå€æŽãã |
function | é¢æ°ã宣èšãã |
class | ã¯ã©ã¹ã宣èšãã |
throw | äŸå€ãã¹ããŒãã |
try/catch/finally | äŸå€ãåŠçããåŸå§æ«ãè¡ã |
debugger | ãããã°çšã®ãã¬ãŒã¯ãã€ã³ã |
import | ä»ã®ã¢ãžã¥ãŒã«ã§å®çŸ©ãããå€ã«å¯ŸããŠååã宣èšãã |
export | ä»ã®ã¢ãžã¥ãŒã«ããã€ã³ããŒãã§ããå€æ°ã宣èšãã |
with | çŸåšã®ã¹ã³ãŒããã§ãŒã³ãæ¡åŒµããïŒå©çšããªãã»ããããããŸãstrict ã¢ãŒãã§ã¯äœ¿çšã§ããªãïŒ |
"use strict" | ã¹ã¯ãªãããŸãã¯é¢æ°ã«å¯ŸããŠstrict ã¢ãŒããé©çšãã |
ifæ
- ifæã䜿çšãããšãã®äžžæ¬åŒ§ã¯å¿ ãèšèŒãã
-
Ruby
ã®elsif
ã¯ãJS
ã ãšelse if
ã§èšè¿°ãã
if (n === 1) {
console.log('æ°å€ã¯1ã§ãã');
} else if (n === 2){
console.log('æ°å€ã¯2ã§ãã');
} else {
console.log('å€ãå
¥åãããŠããŸãã');
}
// å€ãå€ããªããšãã¯äžé
æŒç®åã䜿çšã§ãã
const selectNum = n === 1 ? console.log('æ°å€ã¯1ã§ãã') : console.log('å€ãå
¥åãããŠããŸãã');
â»switchæ(case)
-
else if
ãç°¡æœã«èšè¿°ã§ãã -
case
ã¯éå§ç¹ã¯ç€ºããŠãçµäºç¹ã¯ç€ºããªããbreakæ
ãšã»ãã -
breakæ
ãå®è¡ããããšswitchæ
ã®æåŸã«åŠçã移åããŠããã®æ¬¡ã®æããå®è¡ãåéããã -
switchæ
ãå®è¡ããããšããã¹ãŠã®caseæ
ãå®è¡ãããããã§ã¯ãªãã®ã§ãé¢æ°ã®åŒã³åºããä»£å ¥ã®ãããªå¯äœçšãæã€ãããªåŒã¯é¿ããã - ã§ããã ãå®æ°ã䜿çšããã
switch(n) {
// n === 1ã®å Žå
case 1:
console.log('æ°å€ã¯1ã§ãã');
break; // ããã§äžæãã
case 2:
console.log('æ°å€ã¯2ã§ãã');
break;
default:
// ifæã®elseãšåã圹å²
console.log('å€ãå
¥åãããŠããŸãã');
break;
}
// ãã®æ¬¡ã®æ~~~~
â»whileæ
while (expression)
ãstatement
-
whileæ
ã¯ãã¯ããã«expressionãè©äŸ¡ããã -
false
ã®ãšãã¯ãã«ãŒãæ¬äœãé£ã°ãã次ã®æãžé²ã -
true
ã®ãšãã¯ãstatementãå®è¡ããŠãå ã®expressionã«æ»ã
// countã®å€ãå€ãããªããã°ç¡éã«ãŒãã«ãªã
// äžè¬ã«foræã䜿çšããæ¹ãå€ã
let count = 0;
while(count < 10){
console.log(count);
count ++;
}
foræ
- ã«ãŒãã«ã¯ãªãããã®ã«ãŠã³ã¿å€æ°ããã
- ã«ãŒãåã«å€æ°ãåæåãã
- äžèšã®å€æ°ããã¹ãããŠããã«ãŒãåŠçãè¡ã
for(initialize ; test ; increment)
statement
// initialize ; test ; increment
// åæåããã¹ããæŽæ°
// ããå
·äœã§ã¯ä»¥äžã®ããã«ãªã
for (let count = 0; count < 10; count++) {
console.log(count);
}
- for/ofæ
- for/inæ
ãªã©ããã
breakæ
- ã«ãŒãæãªã©ãéäžã§æ¢ãããæãªã©ã«äœ¿çšãã
- æ®éã¯ãã«ãŒãã®äžã§å¿ èŠãªå€ãèŠã€ãã£ãŠãã«ãŒãã¯ç¶ã
-
break
ã䜿çšãããšèŠã€ãã£ãæç¹ã§ã«ãŒãæãçµã
let i = 0;
while (i < 6) {
if (i === 3) {
break;
}
i = i + 1;
}
console.log(i);
// expected output: 3
continueæ
-
breakæ
ãšäŒŒãŠã -
continueæ
ã¯æ¬¡ã®ç¹°ãè¿ãã«ãŒãããåéãã
let text = [];
for (let i = 0; i < 10; i++) {
if (i === 3) {
continue;
}
text.push(i);
}
console.log(text);
// expected output: Array [0, 1, 2, 4, 5, 6, 7, 8, 9]
returnæ
-
returnæ
ã䜿ããšé¢æ°åŒã³åºãã®å€ãæå®ãã -
returnæ
ã¯é¢æ°æ¬äœã§ãã䜿ããªã - ãã以å€ã§äœ¿çšãããšæ§æãšã©ãŒã«ãªã
-
return
ãå«ãŸãªãé¢æ°ã®å Žåã¯ãé¢æ°æ¬äœãåŸã ã«å®è¡ããŠãããé¢æ°ã«éãããšåŒã³åºãå ã«æ»ãã - äžèšã®å Žåã¯ãåŒã³åºãã®å€ã¯
undefined
ãšè©äŸ¡ãããã - ã»ãšãã©ã®å Žåã¯ãé¢æ°ã®æåŸã«è¿ããã
- éäžã§åŒã³åºããšæã®æ®ã£ãŠããŠãé¢æ°ã®åŒã³åºãå ã«æ»ãã
function getRectArea(width, height) {
if (width > 0 && height > 0) {
return width * height;
}
return 0;
}
console.log(getRectArea(3, 4));
// expected output: 12
console.log(getRectArea(-3, 4));
// expected output: 0
// returnããªããšundefinedãè¿ã
function getRectArea(width, height) {
if (width > 0 && height > 0) {
width * height;
}
return 0;
}
console.log(getRectArea(3, 4));
// expected output: 0
console.log(getRectArea(-3, 4));
// expected output: 0
"use strict"æ
TypeScript
ããçæãããJavaScript
ãã¡ã€ã«ã®äžã§äœ¿çšãããŠããã
6ç« ãªããžã§ã¯ã -5min-
ãªããžã§ã¯ããšã¯ãªããã
ãªããžã§ã¯ãã®åºæ¬
let myCar = {
make: 'Ford',
model: 'Mustang',
year: 1969
}
// 以äžã®ããã«ããªããžã§ã¯ãã宣èšããããšãã§ãã
// ãªããžã§ã¯ãã®åæååã䜿çšããŠããã
// ãªããžã§ã¯ãåæååã¯ãäžæ¬åŒ§ ({}) ã§å²ãŸãããªããžã§ã¯ãã®ããããã£åãšé¢é£ããå€ã® 0 å以äžã®ãã¢ãã«ã³ãã§åºåã£ããªã¹ãã§ãã
lat myCar = new Object();
myCar.make = 'Ford';
myCar.model = 'Mustang';
myCar.yaer = 1969;
// ãªããžã§ã¯ãã«å²ãåœãŠãããŠããªãããããã£ã¯ undefined ãè¿ã (null ã§ã¯ãªã)ã
myCar.color; // undefined
ãªããžã§ã¯ãã®æ¯èŒ
// 2 ã€ã®å€æ°ã¯ãåãããããã£ãæ〠2 ã€ã®ç°ãªããªããžã§ã¯ã
let fruit = {name: 'apple'};
let fruitbear = {name: 'apple'};
fruit == fruitbear; // false ãè¿ããã
fruit === fruitbear; // false ãè¿ããã
// 2 ã€ã®å€æ°ããªããžã§ã¯ã㯠1 ã€
let fruit = {name: 'apple'};
let fruitbear = fruit; // fruitbear ã« fruit ãªããžã§ã¯ããžã®åç
§ã代å
¥
// fruit ãš fruitbear ã¯åããªããžã§ã¯ããæããŠãã
fruit == fruitbear; // true ãè¿ããã
fruit === fruitbear; // true ãè¿ããã
fruit.name = 'grape';
console.log(fruitbear); // { name: "apple" } ã§ã¯ãªã { name: "grape" } ãšåºåããã
7ç« é å -10min-
é åãšã¯
å ·äœçã«ã¯ã以äžã®ãããªãã®
// 以äžã®æã¯åãé
åãçæãã
let arr = new Array(element0, element1, ..., elementN)
let arr = Array(element0, element1, ..., elementN)
let arr = [element0, element1, ..., elementN]
// 空ã®é
åã®äœãæ¹ïŒé
åãªãã©ã«ïŒ
// ãã¹ãŠåãæå³
let arr = new Array()
let arr = Array()
let arr = []
// => undefinedãè¿ã
ãªããžã§ã¯ãã®ããããã£ã«å²ãåœãŠã
let obj = {}
// ...
obj.prop = [element0, element1, ..., elementN]
// ãŸãã¯
let obj = {prop: [element0, element1, ...., elementN]}
é åã§ã®å°æ°ã®æ±ã
let arr = Array(9.3) // RangeError: Invalid array length
// ãšã©ãŒã«ãªã
// `Array.of`ã¡ãœããã䜿çšããã°äœ¿ãã
let wisenArray = Array.of(9.3) // wisenArray 㯠1 ã€ã®èŠçŽ 9.3 ã ããæã€é
å
é åã®ããŒã¿åç §
let myArray = ['Wind', 'Rain', 'Fire']
console.log(myArray[0]) // => Wind
é åã«ããŒã¿ãè¿œå
let emp = []
emp[0] = 'Casey Jones'
emp[1] = 'Phil Lesh'
emp[2] = 'August West'
// => ['Casey Jones', 'Phil Lesh', August West']
// pushã¡ãœããã䜿çšããããšã§ãã§ãã
let emp = ['Casey Jones', 'Phil Lesh']
emp.push('August West')
// => ['Casey Jones', 'Phil Lesh', August West']
é åã®å埩åŠç
// éåžžã®forã¡ãœããã䜿ã
let colors = ['red', 'green', 'blue']
for (let i = 0; i < colors.length; i++) {
console.log(colors[i])
}
// => red
// => green
// => blue
// forEach()ã¡ãœãã
let colors = ['red', 'green', 'blue']
colors.forEach(function(color) {
console.log(color)
})
// red
// green
// blue
// ã¢ããŒé¢æ°ã§ãã£ãšçãèšè¿°ã§ãã
let colors = ['red', 'green', 'blue']
colors.forEach(color => console.log(color))
// red
// green
// blue
ä»ã®é åãæ±ãã¡ãœãã
- å€æ¬¡å é åçãªããšãæ±ãã
- ã€ã³ããã¯ã¹ãæå®ããŠæ±ããã¡ãœãããå€ã
䟿å©ãªã¹ãã¬ããæŒç®å
// é
åãå®çŸ©ãã
let vegetables = ['ãã£ãã', 'ãã¶', 'å€§æ ¹', 'ã«ããã']
console.log(vegetables);
// => Array(4) [ "ãã£ãã", "ãã¶", "å€§æ ¹", "ã«ããã" ]
// ã¹ãã¬ããæŒç®åã䜿ããšä»¥äžã®ããã«foræãªã©äœ¿çšããªããŠãåºåã§ããã
console.log(...vegetables)
// => ãã£ãã
// => ãã¶
// => å€§æ ¹
// => ã«ããã
// ã¹ãã¬ããæŒç®åã䜿ã£ãŠé
åãããŒãžããããšãã§ããã
let vegetables = ['ãã£ãã', 'ãã¶', 'å€§æ ¹', 'ã«ããã'];
let fruits = ['ããã', 'ããã'];
const mergeFood = [...vegetables, ...fruits];
console.log(mergeFood);
// => Array(6) [ "ãã£ãã", "ãã¶", "å€§æ ¹", "ã«ããã", "ããã", "ããã" ]
// ã»ãã«ããããªäœ¿ãæ¹ãã§ãã
let vegetables = ['ãã£ãã', 'ãã¶', 'å€§æ ¹', 'ã«ããã'];
let [firstFood, ...rest] = vegetables;
console.log(firstFood);
// => ãã£ãã
console.log(rest);
// => Array(3) [ "ãã¶", "å€§æ ¹", "ã«ããã" ]
8ç« é¢æ° -15min-
èšèã®å®çŸ©
é¢æ°åŒ
// é¢æ°ã¯ç¡åã«ããããšãã§ãã
const square = function(number) {
return number * number;
}
var x = square(4);
console.log(x); // x ã®å€ã¯ 16 ãšãªã
// ååãæå®ããããšã§ååž°çã«äœ¿çšããïŒèªåèªèº«ãåŒã¶ïŒããšãã§ãã
const factorial = function fac(n) {
return n < 2 ? 1 : n * fac(n - 1)
}
console.log(factorial(3))
ä»ã£ãœãæžãæ¹ã®ã¢ããŒé¢æ°
let a = [
'Hydrogen',
'Helium',
'Lithium',
'Beryllium'
];
// map() ã¡ãœããã¯ãäžããããé¢æ°ãé
åã®ãã¹ãŠã®èŠçŽ ã«å¯ŸããŠåŒã³åºãããã®çµæãããªãæ°ããé
åãçæããã
// éåžžã®èšè¿°
let a2 = a.map(function(s) { return s.length; });
console.log(a2); // logs [8, 6, 7, 9]
// ã¢ããŒé¢æ°
let a3 = a.map(s => s.length);
console.log(a3); // logs [8, 6, 7, 9]
// é¢æ°æ¬äœãreturnæã ãã®å Žåã¯ãreturnãšã»ãã³ãã³ãçç¥ã§ããã
// ã¢ããŒé¢æ°ã®åŒæ°ã1ã€ã ãã®å Žåã¯ãäžžæ¬åŒ§ãçç¥ã§ãã
const polynomial = x =>
x*x + 2*x + 3;
// åŒæ°ã®ãªãã¢ããŒé¢æ°ã¯ãäžžæ¬åŒ§ãèšè¿°ããå¿
èŠããã
const constantFunc = () => 42;
é¢æ°ã®åŒã³åºã
é¢æ°ãå®çŸ©ãããŠããšãã¯ãé¢æ°æ¬äœã®ã³ãŒãã¯å®è¡ãããªãã
ãã®é¢æ°ãåŒã³åºãããæã«å®è¡ãããã
- é¢æ°ãšããŠ
- ã¡ãœãããšããŠ
- ã³ã³ã¹ãã©ã¯ã¿ãšããŠ
-
call
ã¡ãœãããapply
ã¡ãœããã䜿çšããŠéæ¥çã« - JSã®èšèªæ©èœãšããŠæé»çã«
// é¢æ°ãåŒã³åºããšã
square(5);
// é¢æ°ãåŒã³åºããšãã¯ã¹ã³ãŒãå
ã«ãªããã°ãããŸãããã次ã®äŸã®ããã«ãé¢æ°ã®å®£èšãå·»ãäžãã (åŒã³åºãããåŸã«çœ®ã) ããšãã§ãã
console.log(square(5));
/* ... */
function square(n) { return n * n }
é¢æ°ã®ã¹ã³ãŒã
// 以äžã®å€æ°ã¯ã°ããŒãã«ã¹ã³ãŒãã§å®çŸ©
let num1 = 20;
let num2 = 3;
// ãã®é¢æ°ã¯ã°ããŒãã«ã¹ã³ãŒãã§å®çŸ©
function multiply() {
return num1 * num2;
}
multiply(); // 60 ãè¿ã
// // 以äžã®å€æ°ã¯ããŒã«ã«ã¹ã³ãŒãïŒé¢æ°å
ïŒã§å®çŸ©
// ãã®é¢æ°ã¯ã°ããŒãã«ã¹ã³ãŒãã§å®çŸ©
function multiply() {
let num1 = 20;
let num2 = 3;
return num1 * num2;
}
multiply(); // 60 ãè¿ã
// 以äžã®ããã«é¢æ°å€ã§é¢æ°å
ã§å®çŸ©ããå€æ°ãåŒã³åºããšãšã©ãŒã«ãªã
console.log(num1); // Uncaught ReferenceError: num1 is not defined
ã¹ã³ãŒããšé¢æ°ã¹ã¿ãã¯
- ååž°çã«é¢æ°ãåŒã³åºãããæã«äœ¿çšãã
- argumentsãªããžã§ã¯ã
ã¯ããŒãžã£
å åŽã®é¢æ°ãäœãããã®åœ¢ã§å€åŽã®é¢æ°ã®ã¹ã³ãŒãå€ã®ã©ããã§äœ¿çšå¯èœã«ãªã£ãå Žåã«äœããã
var pet = function(name) { // å€åŽã®é¢æ°ã¯å€æ° "name" ãå®çŸ©
var getName = function() {
return name; // å
åŽã®é¢æ°ã¯å€åŽã®é¢æ°ã®å€æ° "name" ã«ã¢ã¯ã»ã¹å¯èœ
}
return getName; // å
åŽã®é¢æ°ãè¿ãããšã§ãå€åŽã®é¢æ°ã«å
¬éãã
}
myPet = pet('Vivie');
myPet();
é¢æ°ã®åŒæ°ãšãã©ã¡ãŒã¿
é¢æ°ã¯åŒã°ã ããããšããåŒæ°ã®æ°ãããã§ãã¯ããªã
ãªã®ã§äžé©åãªåŒæ°ãæž¡ãããªãããã«ããããšãã§ããªãã
ããã§ããã©ã«ãã®åŒæ°ãäžããæ¹æ³ãããã
function multiply(a, b = 1) {
return a * b;
}
multiply(5); // 5
â»æ®äœåŒæ°
äžç¹å®å€æ°ã®åŒæ°ãé åã®ããã«è¡šãããšãã§ãã
function multiply(multiplier, ...theArgs) {
return theArgs.map(x => multiplier * x);
}
var arr = multiply(2, 1, 2, 3);
console.log(arr); // [2, 4, 6]
Argumentsãªããžã§ã¯ã
ES6ã®æ®äœãã©ã¡ãŒã¿ç»å Žåã¯ããã䜿çšããŠããã
åºæ¬çã«ã¯ãïŒä»¥äžã®ãããªèšè¿°ã¯ïŒæ®äœãã©ã¡ãŒã¿ã«æžãæããããïŒ...args
ïŒ
function max(x) {
let maxValue = -Infinity;
for(let i = 0, i < arguments.length, i++){
if(arguments[i] > maxValue) maxValue = arguments[i];
}
return maxValue;
}
max(1, 10, 100, 4, 5, 1000) // => 1000
åŒæ°ã®åå²ä»£å ¥
ãã§ã¹ãšãx軞ãšy軞ã䜿çšãããšããªã©ã«äœ¿ããã
function vectorAdd([x1, y1], [x2, y2]){
return [x1 + x2, y1 + y2];
}
vectorAdd([1, 2], [3, 4]); // => [4, 6]
ã¡ãœãããã§ãŒã³
å€æ°ãçŸ åãããªããŠãã¿ãç°¡æœã«èšè¿°ã§ããã
// ã¡ãœãããã§ãŒã³ã䜿çšããªãã³ãŒã
const a = [ 1 , 2 , 3 , 4 ];
const result = a.map( value=>value*3 );
const result2 = result.filter( value => value % 2 === 0 );
const result3 = result2.join( "," );
console.log( result3 ); // 6,12
// ã¡ãœãããã§ãŒã³ã䜿çšããã³ãŒã
const a = [ 1 , 2 , 3 , 4 ];
const result = a.map( value=>value*3 ).filter( value => value % 2 === 0 ).join( "," );
console.log( result ); // 6,12
æé»çãªé¢æ°ã®åŒã³åºã
- ãªããžã§ã¯ãã«ã²ãã¿ãŒã¡ãœãããšã»ãã¿ãŒã¡ãœãããå®çŸ©ãããŠãå Žåã¯ãããããã£ã®åŒã³åºãã®èšå®ã§ã¡ãœãããåŒã°ããå Žåãããã
- åå€æãªã©æé»çã«è¡ãããæã«
String()
ãªã©åŒã°ãã - å埩å¯èœãªãªããžã§ã¯ãã®èŠçŽ ãã«ãŒãããæã«ããã€ãã®ã¡ãœãããåŒã°ãã
- ã¿ã°ä»ããã³ãã¬ãŒããªãã©ã«ã¯é¢æ°ã®åŒã³åºã
-
Proxy
ãªããžã§ã¯ãã«å¯ŸããŠåŠçãè¡ããšé¢æ°ãåŒã°ãã
å®çŸ©æžã¿é¢æ°
9ç« ã¯ã©ã¹ -5min-
ã¯ã©ã¹ãšã³ã³ã¹ãã©ã¯ã¿
classæ§æã¯ãã³ã³ã¹ãã©ã¯ã¿ãã¡ãœããã®å®çŸ©ãããæã«äœ¿ãããŸãã
ã³ã³ã¹ãã©ã¯ã¿ã¯ã€ã³ã¹ã¿ã³ã¹(å®äœ)ãäœæããé¢æ°ãã¡ãœããã¯ãªããžã§ã¯ãã®äžã«å®çŸ©ãããé¢æ°ã§ããã ã€ãŸãclassæ§æã¯ãè²ã
ãªé¢æ°ãã²ãšãŸãšã ã«ããŠäœ¿ãåããããã«ã§ããã
10ç« ã¢ãžã¥ãŒã« -5min-
ES6ããJSã§import
ãšexport
ããŒã¯ãŒããè¿œå ããã
ES6ã®ãšã¯ã¹ããŒã
// å®æ°ãå€æ°ãé¢æ°ãã¯ã©ã¹ã®ãšã¯ã¹ããŒãã¯å®£èšåã«exportãè¿œå ãã
export const PI = Math.PI;
// è€æ°ããŸãšããŠãšã¯ã¹ããŒããã
export { Circle, degreesToRadians, PI };
// å€ã¯1ã€ã®å Žåã¯ä»¥äžã®ããã«èšè¿°ãããšã€ã³ããŒããããããªã
export default class BitSet {
// å®è£
...
}
ES6ã®ã€ã³ããŒã
// ããã©ã«ããšã¯ã¹ããŒããå®çŸ©ããŠããã¢ãžã¥ãŒã«ãã€ã³ããŒããããšã
import BitSet from './bitset.js';
// è€æ°ã®å€ããšã¯ã¹ããŒãããã¢ãžã¥ãŒã«ããå€ãã€ã³ããŒããããšã
import { mean, stddev } from "./stats.js";
// ã€ã³ããŒãããå€ãå€æŽãããå Žå
import { render as renderImage } from "./imageutils.js";
// åçã«ã€ã³ããŒããå©çšããïŒES2020ïŒ
// éåžžã®ã€ã³ããŒã
import * as stats from "./states.js";
// åçã«ã€ã³ããŒã
import("./stats.js").then(stats => {
let average = stats.mean(date);
})