Arrow 函数
一句话解释
arrow 函数 是 compact 函数 语法 using => 那个 做 不 有 它的 own 这个 or 实参 binding.
用大白话说
Arrow 函数 (introduced in ES6) provide shorter 语法 for 编写函数表达式. 它们 use => arrow: (params) => expression or (params) => { statements }.
Two key differences from regular functions: (1) Arrow functions do not have their own 这个 — they inherit 这个 from the enclosing lexical scope. This makes them ideal for callbacks where you want to preserve the outer 这个. (2) Arrow functions do not have their own 实参 object.
If the function body is a single expression, you can omit the curly braces and 返回 — the expression is implicitly returned. For a single parameter, you can omit the parentheses: x => x * 2.
示例
const square = x => x * x;
const add = (a, b) => a + b;
const greet = (name = "Guest") => `Hello, ${name}`;
console.log(square(5)); // 25
console.log(add(2, 3)); // 5
console.log(greet()); // "Hello, Guest"Arrow 函数 使用 implicit 返回, 默认 params, and concise 语法.
常见混淆
容易混淆的对象: regular 函数
关键区别: Regular 函数 有 它们的 own 这个 (determined 通过 call); arrow 函数 inherit 这个 从 enclosing 作用域. Regular 函数 能 是 constructors; arrow 函数 cannot.
相关术语
相关资源
相关课程
- 函数
学习 关于 arrow 函数.
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。