声明一个回调函数,该函数使用闭包中的函数并在该闭包内执行回调函数抛出错误

时间:2017-10-30 23:17:30

标签: javascript scope closures

这是一个人为的例子,但我只想了解范围如何运作。 有人可以解释为什么以下引发错误?我不确定如果在定义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'))

jsbin link

0 个答案:

没有答案