JavaScript ã¯é常ã«é¢æ°æåãªè¨èªã§ãããããã«ããå¤ãã®èªç±ãããã¾ããããæç¹ã§ä½æããé¢æ°ãå¥ã®å¤æ°ã«ã³ãã¼ãããå¥ã®é¢æ°ã«å¼æ°ã¨ãã¦æ¸¡ããå¾ã§ã¾ã£ããå¥ã®å ´æããå¼ã¶ãã¨ãã§ãã¾ãã
ç§ãã¡ã¯ãã§ã«é¢æ°ããã®å¤ï¼âå¤å´â ã®å¤æ°ï¼ã«ã¢ã¯ã»ã¹ã§ãããã¨ãç¥ã£ã¦ãã¾ã
ã§ãããé¢æ°ãä½æããããã¨ã§å¤å´ã®å¤æ°ãå¤æ´ããã¨ä½ãèµ·ããã§ããããï¼é¢æ°ã¯ææ°ã®å¤ or å¤ãå¤ã©ã¡ããåå¾ããã®ã§ããããï¼
ã¾ããé¢æ°ããã©ã¡ã¼ã¿ã¨ãã¦æ¸¡ãããå¥ã®ã³ã¼ãé¨åããå¼ã³åºãããå ´åãæ°ããå ´æã§ã®å¤å´ã®å¤æ°ã«ã¢ã¯ã»ã¹ã§ããã®ã§ããããï¼
ãããã®ã·ããªãªãããè¤éãªã·ããªãªãç解ããããã«ãç¥èãåºãã¦ããã¾ãããã
let/const
ã®å¤æ°ã«ã¤ãã¦è©±ãã¾ãJavaScript ã§ã¯å¤æ°ã宣è¨ããæ¹æ³ã3éãããã¾ã: let
, const
ï¼ã¢ãã³ãªæ¹æ³ï¼,ã¨var
(éå»ã®åæ®)ã§ãã
- ãã®è¨äºã§ã¯ãä¾ã«ã¯
let
ã使ç¨ãã¾ãã const
ã§å®£è¨ãããå¤æ°ã¯åãããã«åä½ããããããã®è¨äºã¯const
ã«ã¤ãã¦ãå½ã¦ã¯ã¾ãã¾ãã- å¤ã
var
ã¯ããã¤ã大ããªéãããããããã㯠å¤ã "var" ã®è¨äºã§èª¬æãã¦ãã¾ãã
ã³ã¼ããããã¯
å¤æ°ãã³ã¼ãããã㯠{...}
ã®ä¸ã§å®£è¨ãããå ´åã¯ããã®ãããã¯ã®ä¸ã§ã ãè¦ãã¾ãã
ä¾:
{
// å¤ã«ã¯è¦ããå¿
è¦ã®ãªãããã¼ã«ã«å¤æ°ã§å¦çããã
let message = "Hello"; // ãã®ã³ã¼ããããã¯ã§ã ãè¦ãã¾ãã
alert(message); // Hello
}
alert(message); // Error: message is not defined
ããã使ç¨ãã¦ãç¬èªã®ã¿ã¹ã¯ãå®è¡ããã³ã¼ãé¨åãããã®ã¿ã¹ã¯ã«ã®ã¿å±ããå¤æ°ã§åé¢ãããã¨ãã§ãã¾ãã:
{
// ã¡ãã»ã¼ã¸ã表示
let message = "Hello";
alert(message);
}
{
// å¥ã®ã¡ãã»ã¼ã¸ã表示
let message = "Goodbye";
alert(message);
}
åå¨ããå¤æ°å㧠let
ã使ç¨ãããã¨ããã¨ãåé¢ãããããã¯ããªãã¨ã¨ã©ã¼ã«ãªããã¨ã«æ³¨æãã¦ãã ããã:
// ã¡ãã»ã¼ã¸ã²è¡¨ç¤º
let message = "Hello";
alert(message);
// å¥ã®ã¡ãã»ã¼ã¸ã²è¡¨ç¤º
let message = "Goodbye"; // Error: variable already declared
alert(message);
if
, for
, while
ãªã©ã®å ´åãã{...}
ã®ä¸ã§å®£è¨ãããå¤æ°ã¯ããã®å
å´ã§ã ãè¦ãã¾ã:
if (true) {
let phrase = "Hello!";
alert(phrase); // Hello!
}
alert(phrase); // Error, ãã®ãããªå¤æ°ã¯æãã¾ãã!
ããã§ã¯ if
ãçµãã£ããã¨ã§ã次㮠alert
㯠phrase
ã¯è¦ããªããããã¨ã©ã¼ã«ãªãã¾ãã
ããã«ãããif
åå²åºæã®ãããã¯ã¬ãã«ã®ãã¼ã«ã«å¤æ°ãä½æã§ããã®ã§ãç´ æ´ããããã¨ã§ãã
åæ§ã®ãã¨ã for
ã«ã¼ãã¨while
ã«ã¼ãã«ãå½ã¦ã¯ã¾ãã¾ãã
for (let i = 0; i < 3; i++) {
// å¤æ° i ã¯ãã® for ã®ä¸ã§ã ãè¦ãã¾ã
alert(i); // 0, 1, 2
}
alert(i); // Error, no such variable
è¦è¦çã«ã¯ let i
㯠{...}
ã®å¤å´ã«è¦ãã¾ãããããã§ã¯ for
æ§æã¯ç¹å¥ã§ãããã®ä¸ã§å®£è¨ãããå¤æ°ã¯ãããã¯ã®ä¸é¨ã¨ã¿ãªããã¾ãã
ãã¹ããããé¢æ°
å¥ã®é¢æ°ã®å é¨ã§ä½æãããé¢æ°ã¯ãâãã¹ããããâ é¢æ°ã¨å¼ã°ãã¾ãã
JavaScript ã§ã¯ç°¡åã«å®ç¾ã§ãã¾ãã
ããã¯ã次ã®ããã«ã³ã¼ããæ´çããã®ã«å©ç¨ã§ãã¾ã:
function sayHiBye(firstName, lastName) {
// ä¸ã§ä½¿ããã¹ãããããã«ãã¼é¢æ°ã§ã
function getFullName() {
return firstName + " " + lastName;
}
alert( "Hello, " + getFullName() );
alert( "Bye, " + getFullName() );
}
ããã§ã® ãã¹ãããã é¢æ° getFullName()
ã¯å©ä¾¿æ§ã®ããã«ä½ããã¦ãã¾ããããã¯å¤é¨å¤æ°ã«ã¢ã¯ã»ã¹ãããã¨ãã§ããã®ã§ããã«ãã¼ã ãè¿ããã¨ãã§ãã¾ãã
ããã«èå³æ·±ãç¹ã¯ãæ°ãããªãã¸ã§ã¯ãã®ããããã£ï¼å¤é¨é¢æ°ãã¡ã½ãããæã¤ãªãã¸ã§ã¯ããä½æããå ´åï¼ã¾ãã¯ãã®èªèº«ã®çµæã¨ãã¦ããã¹ããããé¢æ°ãè¿ããã¨ãã§ãããã¨ã§ã: ããã¯ä»ã®å ´æã§ä½¿ããã¨ãã§ãã¾ããã©ãã«ãã¦ããåãå¤é¨å¤æ°ã«ã¯ä¾ç¶ã¨ãã¦ã¢ã¯ã»ã¹ã§ãã¾ãã
ä¸è¨ã® makeCounter
ã¯å®è¡ãã¨ã«æ¬¡ã®æ°ãè¿ã âcounterâ é¢æ°ãä½æãã¾ãã
function makeCounter() {
let count = 0;
return function() {
return count++;
};
}
let counter = makeCounter();
alert( counter() ); // 0
alert( counter() ); // 1
alert( counter() ); // 2
åç´ã§ããããã®ã³ã¼ãããããã«å¤æ´ããããªã¢ã³ãã¯å®è·µã§ä½¿ç¨ããã¦ãã¾ããä¾ãã°ãèªåãã¹ãç¨ã®ä¹±æ°ãçæããããã®ä¹±æ°ã¸ã§ãã¬ã¼ã¿(random number generator) ãããã¾ãã
ããã¯ã©ã®ããã«åä½ããã§ããããï¼è¤æ°ã® counter ãä½æããå ´åããããã¯ç¬ç«ãã¾ããï¼å¤æ°ã¯ã©ã®ããã«ãªã£ã¦ããã§ããããã
ãã®ãããªãã¨ãç解ããã®ã¯ãJavaScriptã®å ¨ä½çãªç¥èã«ã¨ã£ã¦ç´ æ´ããããã¨ã§ãããããè¤éãªã·ããªãªã§ã¯æçã§ããã§ã¯ããã詳ããã¿ã¦ããã¾ãããã
ã¬ãã·ã«ã«/èªå½ç°å¢(Lexical Environment)
ãã®å ã«ã¯è©³ç´°ãªæè¡ç説æãããã¾ãã
ä½æ°´æºè¨èªã®è©³ç´°ã¯é¿ãããã¨ããã§ããããããæãã«ãã¦ç解ãããã¨ã¯ä¸ååã§ä¸å®å ¨ãªãã®ã«ãªããããæºåãã¦ãã ããã
ããããããããããã«ãè¤æ°ã®ã¹ãããã«åãã¦èª¬æãã¦ãã¾ãã
Step 1. å¤æ°
JavaScript ã§ã¯ãå®è¡ä¸ã®ãã¹ã¦é¢æ°ãã³ã¼ãããã㯠{...}
ããã³ã¹ã¯ãªããå
¨ä½ã«ã¯ãã¬ãã·ã«ã«ç°å¢ ã¨å¼ã°ããå
é¨ã®ï¼é ããï¼é¢é£ãªãã¸ã§ã¯ããããã¾ãã
ã¬ãã·ã«ã«ç°å¢ãªãã¸ã§ã¯ãã¯2ã¤ã®é¨åããæ§æããã¾ã:
- ç°å¢ã¬ã³ã¼ã(Environment Record) ãããããã£ã¨ãã¦ãã¹ã¦ã®ãã¼ã«ã«å¤æ°ããã¤ãªãã¸ã§ã¯ãã§ã(
this
ã®å¤ãªã©ãä»ã®æ å ±ãããããæã£ã¦ãã¾ã)ã - å¤é¨ã®ã¬ãã·ã«ã«ç°å¢ ã¸ã®åç §ãé常ãç´è¿ã®å¤é¨ã®ã¬ãã·ã«ã«ãªã³ã¼ãã«é¢é£ä»ãããã¦ãã¾ãï¼ç¾å¨ã®æ³¢æ¬å¼§ã®å¤å´ï¼ã
âå¤æ°â ã¯åã«ãç¹å¥ãªå
é¨ãªãã¸ã§ã¯ãã§ãã ç°å¢ã¬ã³ã¼ã
ã®ããããã£ã§ããâå¤æ°ãåå¾ã¾ãã¯å¤æ´ããâ ã¨ã¯ãâãã®ãªãã¸ã§ã¯ãã®ããããã£ãåå¾ã¾ãã¯å¤æ´ããâ ãã¨ãæå³ãã¾ãã
ä¾ãã°ããã®ç°¡åãªã³ã¼ãã§ã¯ãã¬ãã·ã«ã«ç°å¢ã¯1ã¤ã ãããã¾ãã:
ããã¯ãã¹ã¯ãªããå ¨ä½ã«é¢é£ä»ãããããããããã°ãã¼ãã«ã¬ãã·ã«ã«ç°å¢ã§ãã
ä¸ã®å³ã®é·æ¹å½¢ã¯ç°å¢ã¬ã³ã¼ã(å¤æ°ã¹ãã¢)ã§ãç¢å°(outerã®é¨å)ã¯å¤é¨åç
§ãæå³ãã¾ããã°ãã¼ãã«ã¬ãã·ã«ã«ç°å¢ã¯å¤é¨åç
§ãæã£ã¦ããªãã®ã§ã null
ã§ãã
ã³ã¼ããå®è¡ããã¦ããã¨ãã¬ãã·ã«ã«ç°å¢ã¯å¤åãã¦ããã¾ãã
ããã§ã¯å°ãé·ãã³ã¼ããç´¹ä»ãã¾ã:
å³å´ã®é·æ¹å½¢ã¯ãå®è¡ã®éã§ã©ã®ããã«ã°ãã¼ãã«ã¬ãã·ã«ã«ç°å¢ãå¤ãããã示ãã¦ãã¾ãã:
- ã¹ã¯ãªãããéå§ããã¨ãã¬ãã·ã«ã«ç°å¢ã«ã¯å®£è¨ããããã¹ã¦ã®å¤æ°ããããããç¨æããã¦ãã¾ãã
- æåã¯ãâæªåæåâ ã®ç¶æ
ã§ããããã¯ç¹å¥ãªå
é¨ç¶æ
ã§ãã¨ã³ã¸ã³ã¯å¤æ°ã¯ç¥ã£ã¦ãã¾ããã
let
ã§å®£è¨ãããã¾ã§ã¯åç §ã§ããªããã¨ãæå³ãã¾ããå¤æ°ãåå¨ããªãã®ã¨ã»ã¼åãã§ãã
- æåã¯ãâæªåæåâ ã®ç¶æ
ã§ããããã¯ç¹å¥ãªå
é¨ç¶æ
ã§ãã¨ã³ã¸ã³ã¯å¤æ°ã¯ç¥ã£ã¦ãã¾ããã
- ãã®å¾
let phrase
å®ç¾©ãç¾ãã¾ãããä»ã¯åæå¤ããªãã®ã§ãundefined
ãæ ¼ç´ããã¾ãããã®æç¹ãããã®å¤æ°ã使ç¨ãããã¨ãã§ãã¾ãã phrase
ã«å¤ãå²ãå½ã¦ããã¾ããphrase
ã®å¤ãå¤æ´ãã¾ãã
ä»ã®ã¨ãããã¹ã¦ã·ã³ãã«ã«è¦ãã¾ããã
- å¤æ°ã¯ç¹å¥ãªå é¨ãªãã¸ã§ã¯ãã®ããããã£ã§ãç¾å¨ã®å®è¡ãããã¯/é¢æ°/ã¹ã¯ãªããã¨é¢é£ä»ãããã¦ãã¾ãã
- å¤æ°ã使ã£ãä½æ¥ã¯ãå®éã«ã¯ãã®ãªãã¸ã§ã¯ãã®ããããã£ã使ã£ã¦ä½æ¥ãã¦ãã¾ãã
âã¬ãã·ã«ã«ç°å¢â ã¯ä»æ§ä¸ã®ãªãã¸ã§ã¯ãã§ãããã㯠è¨èªä»æ§ ã®ä¸ã§åä½ã®ä»çµã¿ã説æããããã«ããçè«çã«ãåå¨ãããã®ã§ãããã®ãªãã¸ã§ã¯ããã³ã¼ãä¸ã§åå¾ãããç´æ¥æä½ãããã¨ã¯ã§ãã¾ããã
ã¾ããJavaScript ã¨ã³ã¸ã³ã¯ãç®ã«è¦ããæ¯ãèãã¯ãã®ã¾ã¾ãªä¸ã§ãæé©åããããã¡ã¢ãªãç¯ç´ããããã«æªä½¿ç¨ã®å¤æ°ãç ´æ£ããããä»ã®å é¨çãªããªãã¯ãå®è¡ãããã¨ãããã¾ãã
Step 2. é¢æ°å®£è¨
é¢æ°ãå¤æ°ã®ããã«å¤ã§ãã
éãã¯ãé¢æ°å®£è¨ã¯å³åº§ã«å®å ¨ã«åæåããããã¨ã§ã
ã¬ãã·ã«ã«ç°å¢ãä½ãããã¨ããé¢æ°å®£è¨ã¯ããã«ä½¿ç¨ã§ããé¢æ°ï¼å®£è¨ã¾ã§ä½¿ç¨ã§ããªã let
ã¨ã¯ç°ãªãã¾ãï¼ã«ãªãã¾ãã
ãã®ããã宣è¨èªä½ã®åã§ãé¢æ°å®£è¨ã¨ãã¦å®£è¨ãããé¢æ°ãå¼ã³åºããã¨ãã§ãã¾ãã
ä¾ãã°ã以ä¸ã¯é¢æ°ã追å ããã¨ãã®ã°ãã¼ãã«ã¬ãã·ã«ã«ç°å¢ã®åæç¶æ ã§ã:
å½ç¶ããã®åä½ã¯é¢æ°å®£è¨ã«ã®ã¿é©ç¨ãããlet say = function(name)...
ã®ããã«å¤æ°ã«é¢æ°ãå²ãå½ã¦ãé¢æ°å¼ã«ã¯ãã¦ã¯ã¾ãã¾ããã
Step 3. å å¤ã®ã¬ãã·ã«ã«ç°å¢
é¢æ°ãå®è¡ãããã¨ãå¼ã³åºãã®å é ã§ã¯æ°ããã¬ãã·ã«ã«ç°å¢ãèªåçã«ä½ããããã¼ã«ã«å¤æ°ã¨å¼ã³åºããã©ã¡ã¼ã¿ãæ ¼ç´ãã¾ãã
ä¾ãã°ãsay("John")
ã®å ´åããã®ããã«ãªãã¾ãï¼å®è¡ã¯ç¢å°ã§ã©ãã«ä»ããããè¡ã§ãï¼:
é¢æ°å¼ã³åºãä¸ã¯ï¼ã¤ã®ã¬ãã·ã«ã«ç°å¢ãããã¾ã: å é¨ã®ãã®ï¼é¢æ°å¼ã³åºãç¨ï¼ã¨å¤é¨ã®ãã®(ã°ãã¼ãã«)ã§ã:
- å
é¨ã®ã¬ãã·ã«ã«ç°å¢ã¯ç¾å¨ã®
say
ã®å®è¡ã«å¯¾å¿ãã¦ãã¾ãã1ã¤å¤æ°(name
)ãæã£ã¦ãããããã¯é¢æ°ã®å¼æ°ã§ããç§ãã¡ã¯say("John")
ãå¼ã³åºããã®ã§ãname
ã¯"John"
ã§ãã - å¤é¨ã®ã¬ãã·ã«ã«ç°å¢ã¯ã°ãã¼ãã«ã¬ãã·ã«ã«ç°å¢ã§ãå¤æ°
phrase
ã¨é¢æ°èªèº«ãããã¾ãã
å
é¨ã®ã¬ãã·ã«ã«ç°å¢ã¯å¤é¨ã®ãã®ã¸ã® å¤é¨
åç
§ãæã£ã¦ãã¾ãã
ã³ã¼ããå¤æ°ã«ã¢ã¯ã»ã¹ãããã¨ããæåã«å é¨ã®ã¬ãã·ã«ã«ç°å¢ãæ¢ãã¾ãããã®æ¬¡ã«å¤å´ãæ¢ãããã§ã¼ã³ã®æå¾ã«ãªãã¾ã§ç¹°ãè¿ãã¾ãã
ããå¤æ°ãã©ãã«ããªãå ´åãstrict ã¢ã¼ãã§ã¯ã¨ã©ã¼ã«ãªãã¾ããuse strict
ããªããã°ãæªå®ç¾©å¤æ°ã¸ã®ä»£å
¥ã¯ä¸ä½äºææ§ã®ããã«æ°ããã°ãã¼ãã«å¤æ°ãä½æãã¾ãã
ä»åã®ä¾ã§ã©ã®ããã«æ¢ç´¢ããããè¦ã¦ã¿ã¾ããã:
say
ã®å å´ã«ããalert
ãname
ã«ã¢ã¯ã»ã¹ãããã¨ããé¢æ°ã®ã¬ãã·ã«ã«ç°å¢ã®ä¸ããããã«è¦ã¤ãã¾ããphrase
ã«ã¢ã¯ã»ã¹ãããã¨ãããã¼ã«ã«ã«ã¯phrase
ããªãã®ã§ãç¶ãã¦å¤é¨
åç §ãè¡ããã°ãã¼ãã«ã§ãããè¦ã¤ãã¾ãã
Step 4. é¢æ°ãè¿ã
makeCounter
ã®ä¾ã«æ»ãã¾ãããã
function makeCounter() {
let count = 0;
return function() {
return count++;
};
}
let counter = makeCounter();
å makeCounter()
å¼ã³åºãã®æåã«ãæ°ããã¬ãã·ã«ã«ç°å¢ãªãã¸ã§ã¯ããä½æããããã® makeCounter
ã®å®è¡ã®ããã®å¤æ°ãæ ¼ç´ããã¾ãã
ãªã®ã§ãã¡ããã©ä¸ã®ä¾ã®ããã«ã2ã¤ã®ãã¹ãããã¬ãã·ã«ã«ç°å¢ãããã¾ã:
éãã¯ãmakeCounter()
ã®å®è¡ä¸ã« return count++
ã¨ãã1è¡ã ãã®å°ããªå
¥ãåã®é¢æ°ãä½ããããã¨ã§ããã¾ã å®è¡ã¯ãã¦ãã¾ããããä½æã ãã¯ãã¦ãã¾ãã
ãã¹ã¦ã®é¢æ°ã¯ãããããä½æãããã¬ãã·ã«ã«ç°å¢ãè¨æ¶ãã¦ãã¾ãããã¹ã¦ã®é¢æ°ã¯ [[Environment]]
ã¨ããååã®é ãããããã£ããã¡ããã®é¢æ°ãä½æãããã¬ãã·ã«ã«ç°å¢ã¸ã®åç
§ãä¿æãã¾ãã
ãªã®ã§ãcounter.[[Environment]]
ã«ã¯ {count: 0}
ã¬ãã·ã«ã«ç°å¢ã¸ã®åç
§ãããã¾ãããã®ããã«ãã¦ãé¢æ°ã¯ã©ãã§å¼ã°ãã¦ããã©ãã§ä½æãããããè¦ãã¦ããã®ã§ãã[[Environment]]
ã¸ã®åç
§ã¯ï¼é¢æ°ã®çææã«ä¸åº¦ã ãï¼ããã¦æ°¸é ã«è¨å®ããã¾ãï¼
ãã®å¾ãcounter()
ãå¼ã°ããã¨ãããã®å¼ã³åºãã«å¯¾ããæ°ããªã¬ãã·ã«ã«ç°å¢ãä½æããããã®å¤é¨ã¬ãã·ã«ã«ç°å¢ã®åç
§ã¯ counter.[[Environment]]
ããåå¾ããã¾ã:
ããã§ãcounter()
å
ã®ã³ã¼ããå¤æ° count
ãæ¢ãã¨ããæåã«èªèº«ã®ã¬ãã·ã«ã«ç°å¢ãæ¤ç´¢ãã¾ãï¼ããã«ã¯ãã¼ã«ã«å¤æ°ã¯ãªãã®ã§ç©ºã§ãï¼ã次ã«å¤é¨ã® makeCounter()
å¼ã³åºãã®ã¬ãã·ã«ã«ç°å¢ãæ¢ããå¤æ°ãè¦ã¤ãæ´æ°ãã¾ãã
å¤æ°ã¯ããã®å¤æ°ãåå¨ããã¬ãã·ã«ã«ç°å¢ã§æ´æ°ããã¾ãã
å®è¡å¾ã®ç¶æ ããã¡ãã§ã:
counter()
ãä½åº¦ãå¼ã³åºãã¨ãå¤æ° count
㯠2
, 3
ã¨å¢ãã¦ããã¾ãã
éçºè ãä¸è¬çã«ç¥ã£ã¦ããã¹ããä¸è¬çãªããã°ã©ãã³ã°ç¨èª âã¯ãã¼ã¸ã£â ãããã¾ãã
ã¯ãã¼ã¸ã£(closure) ã¯å¤é¨å¤æ°ãè¨æ¶ãããããã«ã¢ã¯ã»ã¹ã§ããé¢æ°ã§ããããã¤ãã®è¨èªã§ã¯ããã¯ä¸å¯è½ããããã¯ãããå®ç¾ããããã«ç¹å¥ãªæ¹æ³ã§é¢æ°ãæ¸ãå¿ è¦ãããã¾ããããããä¸ã§èª¬æããã¨ãããJavaScriptã«ããã¦ã¯ããã¹ã¦ã®é¢æ°ã¯èªç¶ã«ã¯ãã¼ã¸ã£ã§ã(1ã¤ã ãä¾å¤ãããã¾ããããã«ã¤ãã¦ã¯ "new Function" æ§æ ã§èª¬æãã¾ã)ã
ã¤ã¾ã: ãããã¯é ããã [[Environment]]
ããããã£ã使ã£ã¦ã©ãã«ä½æãããã®ããèªåçã«è¦ãã¦ãã¦ããã¹ã¦ãå¤é¨å¤æ°ã«ã¢ã¯ã»ã¹ã§ãã¾ãã
é¢æ¥ã§ããã³ãã¨ã³ãã®éçºè
ããã¯ãã¼ã¸ã£ã¯ä½ã§ããï¼ãã¨ãã質åãåããã¨ããæå¹ãªåçã¯ãã¯ãã¼ã¸ã£ã®å®ç¾©ã¨ãJavaScriptã«ããã¦ã¯ãã¹ã¦ã®é¢æ°ãã¯ãã¼ã¸ã£ã§ãããã¨ãã¾ã [[Environment]]
ããããã£ã¨ã¬ãã·ã«ã«ç°å¢ã®ä»çµã¿ã¨è¨ã£ãæè¡çã«è©³ç´°ãªç¨èªã§ã ã
ã¬ãã¼ã¸ã³ã¬ã¯ã·ã§ã³
é常ãé¢æ°å¼ã³åºããçµãã£ãå¾ããã¹ã¦ã®å¤æ°ã®ã¨ã¨ãã«ãã¬ãã·ã«ã«ç°å¢ã¯ã¡ã¢ãªããåé¤ããã¾ããããã¯ããã¸ã®åç §ãåå¨ããªãããã§ããä»ã®JavaScriptãªãã¸ã§ã¯ãã¨åæ§ã«ãå°éå¯è½ãªéã ãã¡ã¢ãªã«ä¿æããã¾ãã
ããããé¢æ°ã®çµäºå¾ãä¾ç¶ã¨ãã¦å°éå¯è½ãªãã¹ããããé¢æ°ãããå ´åãããã¯ã¬ãã·ã«ã«ç°å¢ã¸ã®åç
§ã§ãã [[Environment]]
ããããã£ãæã¡ã¾ãã
ãã®å ´åãã¬ãã·ã«ã«ç°å¢ã¯é¢æ°ã®å®äºå¾ãä¾ç¶ã¨ãã¦å°éå¯è½ãªã®ã§ãåå¨ãç¶ãã¾ãã
ä¾:
function f() {
let value = 123;
return function() {
alert(value);
}
}
let g = f(); // g.[[Environment]] ã¯ã対å¿ãã f() å¼ã³åºãã®ã¬ãã·ã«ã«ç°å¢
// ã¸ã®åç
§ãä¿æãã¾ã
ãã f()
ãä½åº¦ãå¼ã°ããçµæã®é¢æ°ãä¿æãããå ´åã対å¿ããã¬ãã·ã«ã«ç°å¢ãªãã¸ã§ã¯ããã¾ãã¡ã¢ãªã«æ®ãã¾ããä¸ã®ã³ã¼ãã§ã¯3ã¤ãã¹ã¦:
function f() {
let value = Math.random();
return function() { alert(value); };
}
// é
åã«3ã¤ã®é¢æ°ããããããããã対å¿ãã f() ããã®
// ã¬ãã·ã«ã«ç°å¢ã¨é¢é£ã¥ãã¦ãã¾ã
let arr = [f(), f(), f()];
ã¬ãã·ã«ã«ç°å¢ãªãã¸ã§ã¯ãã¯å°éä¸è½ã«ãªã£ãã¨ãã«æ»ã«ã¾ããã¤ã¾ããå°ãªãã¨ã1ã¤ã®å ¥ãåã«ãªã£ãé¢æ°ããããåç §ãã¦ããéã ãåå¨ãã¾ãã
ä¸ã®ã³ã¼ãã§ã¯ã ãã¹ããããé¢æ°ãåé¤ãããå¾ããããå²ãã§ããã¬ãã·ã«ã«ç°å¢ï¼ã¨ value
ï¼ã¯ã¡ã¢ãªããã¯ãªã¢ããã¾ã:
function f() {
let value = 123;
return function() {
alert(value);
}
}
let g = f(); // g ãçãã¦ããéãå¤ã¯ã¡ã¢ãªã«æ®ãç¶ãã¾ã
g = null; // ...ä»ãã¡ã¢ãªã¯ã¯ãªã¼ã³ã¢ããããã¾ã
ç¾å®ã®æé©å(Real-life optimizations)
ããã¾ã§è¦ã¦ããããã«ãçè«çã«ã¯é¢æ°ãçãã¦ããéããã¹ã¦ã®å¤é¨å¤æ°ãä¿æããã¾ãã
ããããå®éã«ã¯JavaScriptã¨ã³ã¸ã³ã¯ãããæé©åãããã¨ãã¾ããå¤æ°ã®ä½¿ç¨ç¶æ³ãåæããå¤é¨å¤æ°ã使ç¨ããã¦ããªããã¨ãããããããå ´åã¯åé¤ããã¾ãã
V8(Chrome, Edge, Opera)ã®éè¦ãªå¯ä½ç¨ã¯ãã®ãããªå¤æ°ã¯ãããã°ã§ã¯å©ç¨ã§ããªããªããã¨ã§ãã
Chrome㧠Developer Tools ãéãã¦ä¸ã®ä¾ãå®è¡ãã¦ã¿ã¦ãã ããã
ä¸æåæ¢ããã¨ããconsole 㧠alert(value)
ãå
¥åãã¦ãã ããã
function f() {
let value = Math.random();
function g() {
debugger; // in console: type alert( value ); No such variable!
}
return g;
}
let g = f();
g();
ã覧ã®éãããã®ãããªå¤æ°ã¯ããã¾ãã! çè«çã«ã¯ã¢ã¯ã»ã¹ã§ããã¯ãã§ãããã¨ã³ã¸ã³ãæé©åãã¦ãã¾ãã
ããã¯ï¼ããã»ã©æéãããããªãã¨ãã¦ãï¼ããããªãããã°ã®åé¡ã«ã¤ãªããå¯è½æ§ãããã¾ããæå¾ ãã¦ããå¤æ°ã®ä»£ããã«ãåãååã®å¤é¨å¤æ°ãåç §ããã¾ã:
let value = "Surprise!";
function f() {
let value = "the closest value";
function g() {
debugger; // in console: type alert( value ); Surprise!
}
return g;
}
let g = f();
g();
V8 ã®ãã®æ©è½ã¯ç¥ã£ã¦ããã¨è¯ãã§ããããã Chrome/Edge/Opera ã§ãããã°ãã¦ããå ´åãé ããæ©ããããã«ééããã§ãããã
ããã¯ãããã¬ã®ãã°ã§ã¯ãªããV8ã®ç¹å¥ãªæ©è½ã§ããæã å¤ããããããã¾ããããã®ãã¼ã¸ã®ä¾ãå®è¡ãããã¨ã§ããã¤ã§ããã§ãã¯ãããã¨ãã§ãã¾ãã
ã³ã¡ã³ã
<code>
ã¿ã°ã使ã£ã¦ãã ãããè¤æ°è¡ã®å ´åã¯<pre>
ãã10è¡ãè¶ ããå ´åã«ã¯ãµã³ãããã¯ã¹ã使ã£ã¦ãã ãã(plnkr, JSBin, codepenâ¦)ã