Skip to content

Instantly share code, notes, and snippets.

@Bambina-zz
Created July 19, 2015 11:11
Show Gist options
  • Save Bambina-zz/86753499863f8e31fe84 to your computer and use it in GitHub Desktop.
Save Bambina-zz/86753499863f8e31fe84 to your computer and use it in GitHub Desktop.
js local and global scope
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