Created
July 19, 2015 11:53
-
-
Save Bambina-zz/ffd0e33a253522c0cdc4 to your computer and use it in GitHub Desktop.
js block scope (before ES6)
This file contains 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
(function x(){ | |
var local = "local"; | |
if(true){ | |
console.log(local); //=>local | |
var block_scope = "block_scope"; | |
}; | |
console.log(block_scope); //=>block_scope が出力される。上のifブロックで定義した変数block_scopeの中身が出力されている。 | |
})(); | |
console.log(block_scope); //=>ReferenceError: block_scope is not defined 当たり前ですが、上の関数内で定義されたローカル変数は関数内でしか参照できません。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment