Skip to content

Instantly share code, notes, and snippets.

@lyuehh
Created December 27, 2013 02:30
Show Gist options
  • Save lyuehh/8141741 to your computer and use it in GitHub Desktop.
Save lyuehh/8141741 to your computer and use it in GitHub Desktop.
var a = 1; // 全局变量
function b(){
a = 2; // 没有var, 就是全局, a变成2了
console.log(a); // 输出2
a = 3; // a变成3了
setTimeout(function(){ //只要是setTimeout, 它就会异步执行, 就算延迟0秒也会异步, 所以这里不会执行了
console.log(a);
}, 0);
a = 4; // 修改a为4
}
b(); // 调用b(), 会先输出第4行的2
console.log(0); // 然后输出这里的0
// 然后就是b方法里的 第7行, 输出4
// 所以答案就是 2 0 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment