Skip to content

Instantly share code, notes, and snippets.

@onionmk2
Created December 19, 2016 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onionmk2/8a7a356facacde90d0fdfdd8968894d9 to your computer and use it in GitHub Desktop.
Save onionmk2/8a7a356facacde90d0fdfdd8968894d9 to your computer and use it in GitHub Desktop.
function showHoge () {
var hoge;
setTimeout(function delayedFunc() {
hoge = 2
console.log('setTimeout', hoge);
});
hoge = 1
console.log('outer', hoge);
}
showHoge();
// outer 1
// setTimeout 2
var fuga = 0;
document.addEventListener('click', function(){ fuga +=1 })
// click後に watch fuga すると1つづつ増えていく
// ES5には関数スコープしかない。内側→外側にスコープ解決する。
// http://qiita.com/matsuby/items/1010ae38ee6258fd8020
// 非同期でも同じ。
// delayedFuncがshowHogeのhogeを hoge= 2 と更新できるのは、非同期でも同じ。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment