这是一个人为的例子,但我只想了解范围如何运作。
有人可以解释为什么以下引发错误?我不确定如果在定义this.otherThing = otherThing
的上下文中执行回调,为什么需要添加otherThing
。
function doSomething(callback){
// works if the following is added
// this.otherThing = otherThing
function otherThing(val){
return val
}
callback()
}
let callback = someVal => {
console.log('was given:',otherThing(someVal))
}
doSomething(() => callback('some value'))