js之作用域、evenLoop


作用域
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var a = [1]
function fn () {
a[1] = 1000
a = [10]
console.log('fn:', a)
}
fn()
console.log('window:', a)
// 'fn:'[10] => 'window:'[10]

var a = [1]
function fn (a) {
a[1] = 1000
a = [10]
console.log('fn:', a)
}
fn(a)
console.log('window:', a)
// 'fn:'[10] => 'window:'[1, 1000]
EvenLoop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
setTimeout(() => {
console.log('1')
})
var p = new Promise((res, rej) => {
console.log('2')
setTimeout(() => {
console.log(3)
})
res()
})
p.then(() => {
console.log(4)
}).then(() => {
console.log(7)
})
setTimeout(() => {
console.log(5)
})
console.log(6)
// 2 => 6 => 4 => 7 => 1 => 3 => 5