Last active
August 29, 2015 14:12
-
-
Save niwatako/03e2fcc8a48c56843398 to your computer and use it in GitHub Desktop.
JavaScriptにおいて、ローカル変数は前後関係なくスコープ内全体で有効
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var foo = "global"; | |
function hoge(){ | |
foo = "(/・ω・)/" //globalスコープのfoo = "global"を書き換えると見せかけてそうではない | |
var foo = "local"; //ここで var foo を宣言しているのでスコープ内のfooは全てこのローカルなfoo | |
}; | |
hoge(); | |
console.log(foo); //-> global | |
/* | |
_人人 人人 人人 人人_ | |
> (/・ω・)/じゃない < | |
 ̄Y^Y^Y^Y^Y^Y^Y^Y^Y^Y ̄ | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment