Kok*_*oko -1 jquery ecmascript-6
我试图用新的=>表示法将回调绑定到jQuery getJSON命令,但它不起作用.
旧代码(作品)
$.getJSON("js/questions.json", function(data){
console.log("loaded json but lost my scope...");
});
Run Code Online (Sandbox Code Playgroud)
胖箭(不工作)
$.getJSON("js/questions.json", (data) => this.test);
function test(data){
console.log("If we get here we might still have our scope");
}
Run Code Online (Sandbox Code Playgroud)
箭头功能中唯一的问题this就是删除它并传递数据.
$.getJSON("js/questions.json", (data) => test(data));
Run Code Online (Sandbox Code Playgroud)
或者不需要匿名函数只需将函数引用设置为第二个参数.
$.getJSON("js/questions.json", test);
Run Code Online (Sandbox Code Playgroud)