æè¿ãES2020ã§å°å ¥ãããæ°æ©èœã«ã€ããŠæžããŸãã..ããããã®å¯èœæ§ã®ããã€ãã¯èå³æ·±ããã®ã§ããããé©åœçããšåŒã°ããã«å€ãããã®ã¯ãããŸãããæè¿JSä»æ§ãé »ç¹ã«æŽæ°ãããŠããããšãèãããšãããã¯ç解ã§ããŸããæšæºã«åãçµãã§ãã人ã¯ãES6ã¢ãžã¥ãŒã«ãç¢å°é¢æ°ãªã©ã®ç¹å¥ãªãã®ãèšèªã«çµ¶ããå°å ¥ããæ©äŒãå°ãªãããšãããããŸããã ããããããã¯ããã£ã±ãæ°ãããã®ãæçµçã«ãã®èšèªã§è¡šç€ºãããªããšããæå³ã§ã¯ãããŸãããå®ã¯ããããä»æ¥ã話ããããããšã§ããé·æçã«ã¯é©åœçãšèšãã4ã€ã®å¯èœæ§ã«ã€ããŠã話ããããšæããŸãã圌ãã¯çŸåšãããã»ã¹ã®ããŸããŸãªæ®µéã«ãããŸãã
ææ¡ã®èª¿æŽãããã¯ãäžæ¹ã§ã¯JavaScriptã§ãããã衚瀺ã§ããªãå¯èœæ§ãããããšãæå³ããä»æ¹ã§ã¯ããã®ãããªæã®ååšã¯ãããã«ããããããããã€ããã®èšèªã§ãããã«äŒãããšãæåŸ ããŠããŸãã
ãã³ã¬ãŒã¿
ããããæãé »ç¹ã«èšèªã«å«ããããã«æ±ãããããã®åšãã§ããªãã®è©±é¡ãé«ãŸã£ãŠããæ©èœããå§ããŸããæ°å¹Žåã圌ãã¯æåéãã©ãã«ã§ã圌女ã«ã€ããŠæžãããããã¯ãã³ã¬ãŒã¿ã«ã€ããŠã§ãã
ããªãã¯ãã§ã«ãã³ã¬ãŒã¿ã«ç²ŸéããŠãããããããŸãããç¹ã«TypeScriptã§æžãå Žåãããã¯ãæ¬è³ªçã«ãéçºè ãç¬èªã®æ©èœãã¯ã©ã¹ãåã ã®ãã£ãŒã«ãããã³ã¡ãœããã«ãæ³šå ¥ãããŠãæçµçã«ã¯ã©ã¹ãããã°ã©ã å¯èœã«ããæ©èœãæäŸããããšãç®çãšããã¡ã¿ããã°ã©ãã³ã°ã®æŠå¿µã§ãã
次ã®äŸãèŠãŠãã ããã
function sealed(constructor: Function) {
Object.seal(constructor);
Object.seal(constructor.prototype);
}
@sealed
class Greeter {
greeting: string;
constructor(message: string) {
this.greeting = message;
}
greet() {
return "Hello, " + this.greeting;
}
}
ããã§ã¯ãæ éã«é²ããããšã«ããTypeScriptãã³ã¬ãŒã¿ã®äœ¿çšäŸã瀺ããŸãããç§ã¯äž»ã«äžè¬çãªèãã瀺ãããã«ãããè¡ããŸãããäžèšã®ã³ãŒãã¹ããããã§ã¯ããã³ã¬ãŒã¿ãäœæ
sealed
ããŠã¯ã©ã¹ã«é©çšããŸããGreeter
ãã芧ã®ãšããããã³ã¬ãŒã¿ã¯ãé©çšãããã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ã«ã¢ã¯ã»ã¹ã§ããé¢æ°ã§ãïŒãããã¿ãŒã²ããã§ãïŒãã³ã³ã¹ãã©ã¯ã¿ãŒãžã®åç
§ãšObject.seal()
ãã¯ã©ã¹ãæ¡åŒµäžå¯èœã«ããã¡ãœããã䜿çšããŸãã
ãã³ã¬ãŒã¿ãã¯ã©ã¹ã«é©çšãã
@
ã«ã¯ãã¯ã©ã¹å®£èšã®çŽåã«ãã³ã¬ãŒã¿ã®ååãã¢ã€ã³ã³ã§æžãçããŸãããã®çµæãã¯ã©ã¹å®£èšã衚瀺ãããåã«ããã©ãŒã ã®æ§æã衚瀺ãããããšãããããŸããã@[name]
ãã®å Žåã¯ãã®ããã«ãªã@sealed
ãŸãã
ãªãã·ã§ã³ãæå¹ã«ããŠãã®TSã³ãŒããã³ã³ãã€ã«ã
experimentalDecorators
ãã¯ã©ã¹ãããã¿ã€ããå€æŽããããšãããšããã³ã¬ãŒã¿ã®æ©èœã確èªã§ããŸãã
Greeter.prototype.test = "test"; // ERROR
ãã®çµæããã³ã¬ãŒã¿ãå¿ èŠãªçç±ãäžè¬çã«ç解ã§ããããã«ãªããŸããããããããã§1ã€ã®é£ãã質åã«è§ŠããããšæããŸããããã¯è£ 食è ã®ããã®ææ¡ã®æ¿èªã®ç¶æ ã«é¢ä¿ããŸãã
æ£åœãªçç±ãããTypeScriptã䜿çšããŠãã³ã¬ãŒã¿ããã¢ã³ã¹ãã¬ãŒã·ã§ã³ããããšã«ããŸãããéèŠãªã®ã¯ãJavaScriptã«ãã³ã¬ãŒã¿ãåã蟌ããšããææ¡ãæ°å¹Žåã«è¡ããããšããããšã§ãããããŠçŸåšã4ã€ã®ãã¡ã®åæã®ç¬¬2段éã§ãã®ã¿ãã§ãããã®ææ¡ã®ãã³ã¬ãŒã¿ã®æ§æãšæ©èœã®äž¡æ¹ã絶ããå€æŽãããŠããŸããããããããã¯JSã³ãã¥ããã£ããã®æŠå¿µã䜿çšããããšã劚ãããã®ã§ã¯ãããŸãããããã確信ããã«ã¯ãTypeScriptãAngular v2 +ãªã©ã®å·šå€§ãªãªãŒãã³ãœãŒã¹ãããžã§ã¯ããæ€èšããã ãã§ååã§ãã
確ãã«ãããããã¹ãŠã¯ãæéã®çµéãšãšãã«ãææ¡ãçºå±ããã«ã€ããŠãä»æ§ã®éäºææ§ã«é¢é£ããåé¡ã«ã€ãªãããŸããã€ãŸãããã³ã¬ãŒã¿ã®ä»æ§ã¯ãææ¡ãåºãŠãã倧ããé²åããŸãããããŸã å€ãã®ãããžã§ã¯ãã§å®è£ ãããŠããŸãããäžèšã®TypeScriptã®äŸã¯ãå€ãããŒãžã§ã³ã®ææ¡ã®å®è£ ã®ãã¢ã³ã¹ãã¬ãŒã·ã§ã³ã§ãã Angularã«ã€ããŠããBabelã«ã€ããŠãåãããšãèšããŸãïŒãã ããBabelã®å Žåããã®ä»æ§ã®æ°ããããŒãžã§ã³ãå®è£ ããããã®äœæ¥ãçŸåšé²è¡äžã§ãããšèšããŸãïŒãäžè¬ã«ã
decorator
ãªã³ã¯ã«é©ããããŒã¯ãŒããšæ§æã䜿çšããæ°ããããŒãžã§ã³ã®æã¯ããŸã ç®ç«ã£ãçšéãèŠã€ããŠããŸããã
èå¿ãªã®ã¯ããã³ã¬ãŒã¿ã¯ã³ãŒãã®æžãæ¹ãå€ããå¯èœæ§ããããšããããšã§ãããããŠããã³ã¬ãŒã¿ã¯ãŸã åæ段éã«ãããŸããããããã®å€æŽã¯ãã§ã«çŸããŠããŸããããããçŸåšã®ç¶æ³ãèãããšããã³ã¬ãŒã¿ã¯ã³ãã¥ããã£ãæçåããŠããã ãã§ãããå®éã«æ¬æ Œçã«äœ¿çšããæºåã¯ãŸã æŽã£ãŠããªããšæããŸãããã³ã¬ãŒã¿ã«èå³ã®ããæ¹ã¯ãå°ãåŸ ã£ãŠããæ¬çªç°å¢ã«å°å ¥ããããšããå§ãããŸãããã ããç§ã®æšå¥šäºé ã¯ããã³ã¬ãŒã¿ïŒAngularãªã©ïŒã䜿çšãããã¬ãŒã ã¯ãŒã¯ã䜿çšããŠããå Žåã«ã¯é©çšãããŸããã
ã¬ã«ã
ããã§ã¯ãå°ãé床ãèœãšãããã³ã¬ãŒã¿ã»ã©è€éã§ã¯ãªã1ã€ã®æ©èœã«ã€ããŠèª¬æããŸããããç§ãã¡ã¯å°åã«ã€ããŠè©±ããŠããã
ç¬èªã®ã³ãŒããŸãã¯ãµãŒãããŒãã£ã®éçºè ããã®ã³ãŒããå®è¡ããå¿ èŠãããç¶æ³ã«ãã§ã«é¥ã£ãŠããå¯èœæ§ããããŸãããåæã«ããã®ã³ãŒããã°ããŒãã«ç°å¢ã«åœ±é¿ãäžããªãããã«å®è¡ããŠãã ãããå€ãã®ã©ã€ãã©ãªãç¹ã«ãã©ãŠã¶ã©ã€ãã©ãªã¯ãã°ããŒãã«ãªããžã§ã¯ããä»ããŠæ©èœããŸã
window
ããã®çµæããããžã§ã¯ããéçºè
ã®å¶åŸ¡ãè¶
ããŠéåžžã«å€ãã®ã©ã€ãã©ãªã䜿çšããå Žåããããã¯äºãã«å¹²æžããå¯èœæ§ããããŸããããã¯ãšã©ãŒã«ã€ãªããå¯èœæ§ããããŸãã
ãã®åé¡ã«å¯ŸããçŸåšã®ãã©ãŠã¶ã®è§£æ±ºçã¯ãèŠçŽ ã䜿çšããããšã§ã
<iframe>
ãããã³ããã€ãã®ç¹å¥ãªå Žå-Webã¯ãŒã«ãŒã®äœ¿çšã Node.jsç°å¢ã§ã¯ãã¢ãžã¥ãŒã«ãŸãã¯åããã»ã¹vm
ã䜿çšããŠåãåé¡ã解決ããŸãã Realms APIã¯ããã®ãããªåé¡ã解決ããããã«èšèšãããŠããŸãã
ãã®APIã¯ãéçºè ãã¹ã³ãŒããšåŒã°ããåå¥ã®ã°ããŒãã«ç°å¢ãäœæã§ããããã«ããããšãç®çãšããŠããŸãããã®ãããªåé åã«ã¯ãç¬èªã®ã°ããŒãã«ãšã³ãã£ãã£ããããŸãã次ã®äŸãèŠãŠãã ããã
var x = 39;
const realm = new Realm();
realm.globalThis.x; // undefined
realm.globalThis.x = 42; // 42
realm.globalThis.x; // 42
x; // 39
ããã§ã¯
Realm
ãé©åãªã³ã³ã¹ãã©ã¯ã¿ãŒã䜿çšããŠæ°ãããªããžã§ã¯ããäœæããŸããä»åŸã¯ãããããã£globalThis
ïŒES2020ã§å°å
¥ïŒãä»ããŠæ°ããã¹ã³ãŒããšãã®ã°ããŒãã«ãªããžã§ã¯ãã«å®å
šã«ã¢ã¯ã»ã¹ã§ããããã«ãªããŸããããã§ã¯ãã¡ã€ã³ã®ãã€ã³ãã¥ããŒã¿ãŒãã®å€æ°ããäœæããã¹ã³ãŒãå
ã®å€æ°ããåé¢ãããŠããããšãããããŸãã
å šäœãšããŠãRealms APIã¯éåžžã«åçŽãªã¡ã«ããºã ãšããŠèšç»ãããŠããŸãããããã¯äŸ¿å©ãªã¡ã«ããºã ã§ãããã®APIã«ã¯ãéåžžã«ç¹æ®ãªäžé£ã®ãŠãŒã¹ã±ãŒã¹ããããŸããããã¯ç§ãã¡ã«é«ã¬ãã«ã®ã»ãã¥ãªãã£ããŸãã¯ãã«ãã¹ã¬ããã³ãŒããžã®æ©èœãæäŸããŸãããããããã·ã¹ãã ã«äžèŠãªè² è·ããããããšãªããåé¢ãããç°å¢ãäœæããããã®åºæ¬çãªæ©èœãæäŸããããšã§ããã®äž»èŠãªã¿ã¹ã¯ã«å®å šã«å¯ŸåŠããŸãã
Realms APIã®ææ¡ã¯ãçŸåšäº€æžã®ç¬¬2段éã«ãããŸããæçµçã«æšæºã«éãããšãã°ããŒãã«ã¹ã³ãŒãã«äŸåãããéããã©ã€ãã©ãªããªã³ã©ã€ã³ã®ãµã³ãããã¯ã¹åãããã³ãŒããšãã£ã¿ãããŸããŸãªã¢ããªã±ãŒã·ã§ã³ã察象ãšããã¢ããªã±ãŒã·ã§ã³ã§äœ¿çšãããããšãæåŸ ãããŸãããã¹ãã
åŒãè¡ã
JavaScriptæ§æã«ã¯ãã»ãšãã©ã®ããã°ã©ãã³ã°èšèªã®æ§æãšåæ§ã«ãã¹ããŒãã¡ã³ããšåŒãå«ãŸããŠããŸãããããã®æ§æã®æãé¡èãªéãã¯ãåŒãå€ãšããŠäœ¿çšã§ããããšã§ãïŒã€ãŸããåŒãå€æ°ã«å²ãåœãŠãããé¢æ°ã«æž¡ãããããããšãã§ããŸãïŒããã¹ããŒãã¡ã³ãã¯ãã®ããã«äœ¿çšã§ããŸããã
ãã®éãããããããããã¯ãªãŒã³ã§ã³ã³ãã¯ããªã³ãŒããäœæããã«ã¯ãåŒãæã奜ãŸããéžæè¢ã§ãã JavaScriptã§ã¯ãç¢å°é¢æ°ãå«ãé¢æ°åŒã®äººæ°ãšãé¢æ°å®£èšãé¢æ°ã¹ããŒãã¡ã³ããæ¯èŒããããšã§ãããã確èªã§ããŸããé åãå埩åŠçããæ¹æ³ãæ¯èŒãããšãåãç¶æ³ãèŠãããŸãïŒ
forEach()
ïŒãµã€ã¯ã«ãããäžå
æŒç®åãšåœä»€ãæ¯èŒããå Žåãããé«åºŠãªããã°ã©ããŒã«ãåãããšãèšããŸãif
ãçŸåšäº€æžæ®µé1ã«ããdo-expression
ææ¡ã¯ãJSåŒã®æ©èœãããã«åŒ·åããããšãç®çãšããŠããŸããã¡ãªã¿ã«ããdo-expressionãã®æŠå¿µãã«ãŒããšæ··åããªãã§ãã ãããã«ãŒãã¯ãŸã£ããç°ãªããã®ã§ãã 次ã«äŸã瀺ããŸãã
doâŠwhile
let x = do {
if (foo()) {
f();
} else if (bar()) {
g();
} else {
h();
}
};
do-expressionå¥ã®æ§æã¯æ¬¡ã®ãšããã§ããäžè¬ã«ãç§ãã¡ã®åã«ã¯ãæ§é ã§ã©ãããããJSã³ãŒãããããŸã
do {}
ããã®æ§æã®æåŸã®åŒã¯ãdoåŒå
šäœã®æçµå€ãšããŠãè¿ãããŸããã
åæ§ã®ïŒãã ãåäžã§ã¯ãªãïŒå¹æã¯ãIIFEïŒImmediately Invoked Function ExpressionïŒã䜿çšããŠå®çŸã§ããŸããããããdoåŒã®å Žåããã®ã³ã³ãã¯ããªæ§æã¯éåžžã«é åçã§ããããã§ã¯ãåæ§ã®æ©èœã§
return
ãããŸãã¯ã®ãããªéãè£å©æ§é ã¯å¿
èŠãããŸãã(() => {})()
..ãããããåŒãæšæºã«å
¥ããšãJavaScriptãžã®åœ±é¿ãç¢å°é¢æ°ã®åœ±é¿ã«å¹æµãããšç§ãä¿¡ããçç±ã§ããåŒã®äŸ¿å©ããšããããããæ§æãããã°ã1ã€ã®ããã±ãŒãžã§éåžžã«é
åçã«èŠããŸãã
ãã¿ãŒã³ãããã³ã°
ãã®æ©äŒã¯ãã®è³æã®æåŸã§ãããéèŠæ§ã¯æåŸã§ã¯ãããŸãããããã¯ãèšèªã«ãã¿ãŒã³ãããã³ã°ã¡ã«ããºã ãå°å ¥ããããšãç®çãšããææ¡ã§ãã
ããªãã¯JSåœä»€ã«ç²ŸéããŠãããããããŸãã
switch
ãã«äŒŒãŠif-else
ããŸããããã®ãªãã·ã§ã³ã¯ããå°ãå¶éãããŠãããå€ãã®éžæè¢ã®1ã€ãéžæããã®ã«é©ããŠããŸããããã¯ãããã©ã®ããã«èŠãããã§ãïŒ
switch (value) {
case 1:
// ...
break;
case 2:
// ...
break;
case 3:
// ...
break;
default:
// ...
break;
}
å人çã«ã¯ãæž¡ããããã®ãç¹å®ã®å€ãšããæ¯èŒã§ããªãã®ã§ãåœä»€ã¯åœä»€
switch
ããã匱ããšæããŸãããã®å¶éã¯åé¿ã§ããŸãããçç±ã¯ããããŸãããããã«ãåœä»€ã¯è£å©èŠçŽ ã§éè² è·ã«ãªã£ãŠããŸããç¹ã«ãæ瀺ã«ã€ããŠè©±ããŠããã
ãã¿ãŒã³ãããã³ã°ã¡ã«ããºã ã¯ãã¹ããŒãã¡ã³ãã®ããæ©èœçã§ãåŒããŒã¹ã®ãæœåšçã«ããçšéã®åºãããŒãžã§ã³ãšèããããšãã§ããŸããåã«å€ãæ¯èŒããã®ã§ã¯ãªãããã¿ãŒã³ãããã³ã°ã¡ã«ããºã ã«ãããéçºè
ã¯é«åºŠã«ã«ã¹ã¿ãã€ãºå¯èœãªã«ã¹ã¿ã ãã¿ãŒã³ïŒãã³ãã¬ãŒãïŒãšå€ãæ¯èŒã§ããŸããææ¡ãããAPIã®äŸã瀺ãã³ãŒãã¹ããããã次ã«ç€ºããŸããif
switch
switch
break
switch
const getLength = vector => case (vector) {
when { x, y, z } -> Math.hypot(x, y, z)
when { x, y } -> Math.hypot(x, y)
when [...etc] -> vector.length
}
getLength({x: 1, y: 2, z: 3})
ããã¯ãJavaScriptã§ã¯éåžžã«çããæ§æã䜿çšããŸãïŒãã ããRustãScalaãªã©ã®èšèªã§èŠãããæ§æã«åºã¥ããŠããŸãïŒãããã¯ããã§ã«ç¥ãããŠããåœä»€ãšããã€ãã®é¡äŒŒç¹ããããŸã
switch
ãããã§ã¯ãããŒã¯ãŒãã®ä»£ããã«ãå€ããã§ãã¯ããããããã¯ã®éå§ã瀺ãåèªswitch
ã䜿çšããcase
ãŠããŸãã次ã«ããããã¯å
ã§ãããŒã¯ãŒãã䜿çšããŸãwhen
ãå€ãæ€èšŒããããã®ãã³ãã¬ãŒãã«ã€ããŠèª¬æããŸãããã³ãã¬ãŒãã®æ§æã¯ããã®èšèªã§ãã§ã«å©çšå¯èœãªãªããžã§ã¯ãç Žå£ã¡ã«ããºã ã®æ§æã«äŒŒãŠããŸããå€ã¯ãéžæããããããã£ãå«ããªããžã§ã¯ããšæ¯èŒã§ãããªããžã§ã¯ãã®ããããã£å€ãä»ã®å€ãã®ãšã³ãã£ãã£ãšæ¯èŒã§ããŸãããã®ã¡ã«ããºã ã®è©³çŽ°ã«ã€ããŠã¯ããã®ããã¥ã¡ã³ããã芧ãã ããã
ãã³ãã¬ãŒãã®èª¬æã®åŸ
->
ã«ãåŒãæãç¢å°ïŒããã©ããç¢å°ãïŒããããŸãïŒé è¿æ³ã§ã¯ãå¥ã®å€ã§ãïŒãããã¯ããã¿ãŒã³ãšã®äžèŽãèŠã€ãããšãã«è©äŸ¡ããå¿
èŠããããŸãã
JSã«ãã®ãããªæ©èœãããã°ãããã°æ°äžä»£ã®ã³ãŒããæžãããšãã§ãããšæããŸãããã ããææ¡ãããæ§æã¯ãèšèªã«ãŸã£ããæ°ããæ§é ãå€æ°å°å ¥ãããããç§ã«ã¯å°ãé¢åã«æããŸãããããŠããã®ææ¡ããŸã åæã®ç¬¬äžæ®µéã«ãããšããäºå®ã¯ããŸã æ¹åã®äœå°ããããšç§ã¯æããŸãããã®æ©èœã¯éåžžã«ææã«èŠããŸãããå ¬åŒã®JSä»æ§ã«å ¥ããŸã§ã«ã¯ãŸã é·ãéã®ãããããŸãã
çµæ
ããã§ãJavaScriptã®é©æ°çãªæ©èœã«ã€ããŠã®ç§ã®è©±ã¯çµããã§ããããã¯ãå°æ¥ãã®èšèªã§èŠãããå¯èœæ§ããããŸããä»ã«ãåæ§ã®å¯èœæ§ããããŸããããšãã°ãå€éšæšæºã©ã€ãã©ãªãšãã€ãã©ã€ã³åãããæŒç®åã®ææ¡ã§ããããããç§ã¯ãã®è³æã®ããã«ãç§ã«ãšã£ãŠæãèå³æ·±ããšæããããã®ã ããéžã³ãŸããããããã®æ©äŒã¯ãŸã ææ¡æ®µéã«ããããšã«çæããŠãã ããããããã¯æéãšãšãã«å€åããå¯èœæ§ããããŸãããŸãã¯ã圌ããæšæºã«å ¥ããªãããšãèµ·ãããããããŸããããããããããã«ããããã®ãããªæ©äŒãä»ã®äººãããå ã«å©çšãããã®ã§ããã°ãããã«ãªã©ã®ãããžã§ã¯ãã詳ãã調ã¹ãããšããå§ãããŸãã..ããããã®ãããžã§ã¯ãã¯ãå€ãã®JSææ¡ïŒç¹ã«æ§æã«é¢é£ããææ¡ïŒãçã¿åºããŸããããã«ãããèšèªã®å®è£ ã«è¡šç€ºããããã£ãšåã«ã誰ã§ãææ°ã®æ©èœãè©Šãããšãã§ããŸãã
JavaScriptã§æãèŠéããŠããæ©èœã¯äœã§ããïŒ