首页 > 技术文章 > js的箭头函数等价于什么

lyzz1314 2021-02-15 17:02 原文

原文:
Arrow functions bind this to the context in which you create the arrow function.

const foo = (args) => {/* code */};

is a shortcut for

const foo = (function(args) {/* code */}).bind(this));

所以说箭头函数是绑定了this的一个普通函数
这方便了异步函数的调用(有时在异步函数中调用this会导致this为null):

 const callback = someobject.somefunction.bind(someobject);
 loader.load(callback); // 成功调用some object.somefunction

 const callback = someobject.somefunction;
 loader.load(callback); // 这时候callback为null

推荐阅读