JavaScript 函数
函数 declarations, arrow 函数, 参数, 返回值, and 闭包.
你将学到
- 函数 declarations 对比函数表达式对比 arrow 函数
- 参数, 默认值, and REST 参数 (...args)
- 返回语句 and 为什么 forgetting 它返回 undefined
- 如何这个 behaves in regular 对比 arrow 函数
- 什么闭包是 and 为什么它们是 useful
概念讲解
Three Ways to Write a Function
// 1. Function declaration — hoisted, can be called before definition
function add(a, b) {
return a + b;
}
// 2. Function expression — not hoisted
const subtract = function (a, b) {
return a - b;
};
// 3. Arrow function — concise, no own this
const multiply = (a, b) => a * b;
Parameters and Defaults
Functions accept parameters and can give them default values:
function greet(name, greeting = "Hello") {
return `${greeting}, ${name}!`;
}
greet("Ada"); // "Hello, Ada!"
greet("Ada", "Hi"); // "Hi, Ada!"
The rest parameter ...args collects any extra arguments into an array:
function sum(...numbers) {
return numbers.reduce((total, n) => total + n, 0);
}
sum(1, 2, 3, 4); // 10
Return Values
A function returns undefined if it has no 返回 statement or if 返回 is used alone. Forgetting 返回 is one of the most common beginner mistakes:
function double(x) {
x * 2; // ← forgot return! Returns undefined
}
function doubleFixed(x) {
return x * 2; // correct
}
Arrow Functions and this
Arrow functions do not have their own 这个 — they inherit it from the surrounding scope. This makes them perfect for callbacks, but bad for object methods that need 这个:
const obj = {
value: 42,
regular: function () { return this.value; }, // 42
arrow: () => this.value, // undefined — no own this
};
Closures
A closure is a function that remembers the variables from where it was created, even after the outer function has returned. This is how you create private state:
function makeCounter() {
let count = 0;
return () => ++count; // "remembers" count
}
const counter = makeCounter();
counter(); // 1
counter(); // 2
counter(); // 3
Closures power many patterns: module factories, memoization, partial application, and React hooks.
示例代码
// Function declaration with default parameters
function greet(name, greeting = "Hello") {
return `${greeting}, ${name}!`;
}
console.log(greet("Ada")); // "Hello, Ada!"
console.log(greet("Ada", "Hi")); // "Hi, Ada!"
// Arrow function — concise for short transformations
const square = (n) => n * n;
console.log(square(5)); // 25
// Rest parameter collects extra arguments
const sum = (...nums) => nums.reduce((a, b) => a + b, 0);
console.log(sum(1, 2, 3, 4)); // 10
// Closure: a function that remembers its creation scope
function makeAdder(base) {
return (n) => base + n; // remembers base
}
const addTen = makeAdder(10);
console.log(addTen(5)); // 15
console.log(addTen(20)); // 30
这个示例 shows declaration 使用 defaults, arrow 函数, REST 参数, and 闭包 (makeAdder). 闭包 let 你创建 configurable 函数那个 remember 它们的设置.
动手试一试
- Math Evaluator
Evaluate mathematical 表达式 — 相似到 什么函数返回.
- Regex Tester
函数经常 wrap regex logic — 测试模式 live.
常见错误
错误写法
Forgetting 返回 keyword
正确做法
函数不使用 explicit 返回语句返回 undefined. 如果你的函数应该 produce 值, 制作确定到 编写返回值;.
错误写法
Using arrow 函数 as 对象方法 and expecting 这个到 工作
正确做法
Arrow 函数做 不有 它们的 own 这个 — 它们 inherit 它从 surrounding 作用域. Use regular 函数 or 方法 shorthand for 对象方法那个需要这个.
相关资源
相关代码片段
- 数组映射 / 筛选 / Reduce
函数通过到 数组方法 — very 常见模式.
相关速查表
- JavaScript Cheatsheet
函数语法, arrow 函数, and 闭包模式.
练习 JavaScript 函数
17 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。