我正在看一些frontend guidelines在 GitHub 上看到了以下代码示例:
// good
const createDivs = howMany => {
if (!howMany) return;
document.body.insertAdjacentHTML("beforeend", "<div></div>");
return createDivs(howMany - 1);
};
createDivs(5);
=>
是什么意思?做什么,它叫什么?我以前从未见过这个成语。我尝试查找它,但不知道它的名字和 MDN documentation不显示。
最佳答案
摘自 MDN 文档 arrow功能:
An arrow function expression (also known as fat arrow function) has a shorter syntax compared to function expressions and lexically binds the this value. Arrow functions are always anonymous.
除了它是一种更简洁的匿名函数编写方式之外,箭头函数还具有在函数表达式中绑定(bind) this
的优点。因此,通常与 bind
一起使用的模式:
document.addEventListener('click', function myHandler (event) {
console.log(event);
}.bind(this));
变成了
document.addEventListener('click', (event) => console.log(event));
关于JavaScript => 运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32426482/