脑图
可以根据以下代码感受作用域链:
function bar() {
var myName = " a "
let test1 = 100
if (1) {
let myName = " b "
console.log(test)
}
}
function foo() {
var myName = " c "
let test = 2
{
let test = 3
bar()
}
}
var myName = " d "
let myAge = 10
let test = 1
foo()
闭包代码
在这里插入代码片
function foo() {
var myName = " a "
let test1 = 1
const test2 = 2
var innerBar = {
getName:function(){
console.log(test1)
return myName
},
setName:function(newName){
myName = newName
}
}
return innerBar
}
var bar = foo()
bar.setName(" b ")
bar.getName()
console.log(bar.getName())