JavaScript函数按定义和行为分为四类:函数声明(完全提升、有名字)、函数表达式(仅变量名提升、可匿名)、箭头函数(无this/arguments/new.target)、生成器函数(function* + yield)。
JavaScript 函数不是一种“类型”,而是一种可调用的对象(Function 类型);但按定义方式、行为特征和运行时表现,确实存在几类常见函数形态——它们的区别直接影响 this 绑定、提升行为、作用域、是否可构造等关键行为。
函数声明会被完整提升到当前作用域顶部,可在定义前安全调用;函数表达式只提升变量名(var 声明),赋值本身不提升,提前调用会报 TypeError: xxx is not a function。
console.log(add(2, 3)); // ✅ 输出 5
function add(a, b) { return a + b; }
console.log(sub(2, 3)); // ❌ TypeError: sub is not a function
const sub = function(a, b) { return a - b; };
this,也没有 arguments 和 new.target
箭头函数不绑定 this,而是继承外层普通函数作用域的 this;它不能用作构造函数(调用 new 会抛 TypeError),也不支持 arguments 对象(需用剩余参数 ...args 替代)。
const obj = {
name: 'Alice',
regular() { console.log(this.name); }, // ✅ 'Alice'
arrow: () => { console.log(this.name); } // ❌ undefined(this 指向全局或 module 上下文)
};
obj.regular(); // 'Alice'
obj.arrow(); // undefined(浏览器中 this 是 window)
array.map(x => x * 2)),避免手动 .bind(this)
this 的场景(如事件处理器、Vue/React 方法、原型方法)yield,所以不能是生成器函数function* 返回迭代器,支持暂停与恢复生成器函数用 function* 声明,内部用 yield 暂停执行并产出值;调用后不立即运行,而是返回一个迭代器对象,需用 .next() 手动推进。
function* countdown(n) {
while (n > 0) {
yield n;
n--;
}
}
const it = countdown(3);
console.log(it.next().value); // 3
console.log(it.next().value); // 2
console.log(it.next().value); // 1
async/await 模拟)new 调用,也不是普通函数的子类型new,行为天壤之别构造函数本质仍是函数,但约定以大写字母开头,并预期通过 new 调用;此时会自动创建新对象、绑定 
this、隐式返回该对象(除非显式返回非空对象)。
function Person(name) {
this.name = name;
}
const p = new Person('Bob'); // ✅ this 指向新对象
const q = Person('Charlie'); // ❌ this 指向全局(非严格模式)或 undefined(严格模式)
new 调用构造函数,极大概率造成意外的全局污染或 undefined 属性赋值class 语法替代传统构造函数,更明确意图且自带 new 检查(class 构造器不可直接调用)Function 构造器(new Function('a', 'b', 'return a+b'))应避免使用——无法访问闭包、存在安全与性能隐患函数类型之间的边界有时很模糊(比如箭头函数也是函数表达式的一种),真正重要的是你在什么上下文中怎么用它。最容易被忽略的,其实是提升规则和 this 绑定这两点——它们往往不会立刻报错,却会在重构或移动代码时突然暴露问题。