js函数有哪几种声明方式?有什么区别?
参考答案
- 函数声明:
function f() {},整体提升,可在定义前调用; - 函数表达式:
const f = function () {},不提升(变量提升但未赋值),可匿名; - 箭头函数:
const f = () => {},无 this/arguments/prototype,不能 new; - 方法简写:
{ f() {} }、class A { f() {} }(不可 new,super 绑定); - 构造函数:
new Function('a', 'return a'),运行时编译,作用域仅全局; - Generator:
function* g() {}; - Async:
async function f() {}(返回 Promise)。
区别核心:提升、this 绑定、能否 new、是否有 arguments/prototype、作用域。