← 返回完整题库
JavaScript简单

js函数有哪几种声明方式?有什么区别?

参考答案

  1. 函数声明:function f() {},整体提升,可在定义前调用;
  2. 函数表达式:const f = function () {},不提升(变量提升但未赋值),可匿名;
  3. 箭头函数:const f = () => {},无 this/arguments/prototype,不能 new;
  4. 方法简写:{ f() {} }、class A { f() {} }(不可 new,super 绑定);
  5. 构造函数:new Function('a', 'return a'),运行时编译,作用域仅全局;
  6. Generator:function* g() {};
  7. Async:async function f() {}(返回 Promise)。

区别核心:提升、this 绑定、能否 new、是否有 arguments/prototype、作用域。

本题来自 计算机知识面试题库,共 921 道题。