胖箭头功能无法使用jquery getJSON?

时间:2016-06-14 08:53:50

标签: jquery ecmascript-6

我正在尝试使用new =>将回调绑定到jQuery getJSON命令符号,但它不起作用。

旧代码(作品)

$.getJSON("js/questions.json", function(data){
   console.log("loaded json but lost my scope...");
});

胖箭(不工作)

$.getJSON("js/questions.json", (data) => this.test);
function test(data){
     console.log("If we get here we might still have our scope");
}

1 个答案:

答案 0 :(得分:5)

箭头功能中唯一的问题是this只需删除它并传递数据。

$.getJSON("js/questions.json", (data) => test(data));

或者不需要匿名函数只需将函数引用设置为第二个参数。

$.getJSON("js/questions.json", test);
相关问题