Skip to content

函数

一句话解释

函数 是 reusable 块 的 code 那个 takes 输入 (参数), 执行 任务, and 可选地 返回 值.

用大白话说

A function is a named, reusable block of code. You define it once and call it many times with different inputs. Functions are the primary way to organize and reuse logic in JavaScript.

Functions can be declared several ways: function declarations (function foo() {}), function expressions (const foo = function() {}), and arrow functions (const foo = () => {}). Each has slightly different behavior regarding hoisting and this binding.

Functions are first-class in JavaScript — they can be assigned to variables, passed as arguments, and returned from other functions. This makes higher-order functions, callbacks, and functional programming patterns possible.

示例

javascript
function add(a, b) {
  return a + b;
}
const multiply = (a, b) => a * b;

console.log(add(2, 3));      // 5
console.log(multiply(4, 5)); // 20

函数 declaration and arrow 函数 表达式 — 两者都 是 函数.

相关术语

相关资源

相关课程

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表