Javascript拼图 - 一个班轮

Bon*_* V. 3 javascript callback

你能解决这个问题而不会抛出错误吗?答案是单行.这是来自一个死的职位发布,答复是在答复中要求的.我认为这是一种清除受访者的聪明方法,但我似乎无法在没有错误的情况下回答这个问题.

明显的解决方案:

f.moo(alert(f.foo));
Run Code Online (Sandbox Code Playgroud)

但那引发了 TypeError: callback is undefined { message="callback is undefined", more...}

var f = (function(){
  return {
    foo : "bar",
    moo : function(callback){
      callback.call(this)
    }
  }
})();
//alert "bar" by foo
Run Code Online (Sandbox Code Playgroud)

Vot*_*ple 10

你必须传递f.moo一个功能.你正在调用alert并传递结果alert(没什么).

f.moo(function() { alert(this.foo); });
Run Code Online (Sandbox Code Playgroud)