Created
July 19, 2015 11:11
-
-
Save Bambina-zz/86753499863f8e31fe84 to your computer and use it in GitHub Desktop.
js local and global scope
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
var scope = 'lalala'; //グローバル変数を定義する。scriptのどこからでも参照できる。 | |
function x(){ //ローカル変数local、グローバル変数globalを定義する。関数内でvarをつけずに定義すると、グローバル変数になる。 | |
var local = 'local'; | |
global = 'global'; | |
console.log(scope, local, global); | |
}; | |
x() //=>lalala local global undefined を返す。 | |
(function (){console.log(local);})(); //関数の即時実行形式。ReferenceError: local is not definedになる。 | |
(function (){console.log(global);})(); //=>global undefined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment