
普通函数:function 关键字声明
箭头函数:() => {} 简写形式,支持多种缩写
// 普通函数
function fn1(x) {
return x + 1;
}
// 箭头函数
const fn2 = x => x + 1;this 由调用方式决定,运行时动态改变示例对比:
const obj = {
name: "测试",
// 普通函数:this → 调用者 obj
fun1() {
console.log(this.name); // 测试
},
// 箭头函数:this 继承外层(window/全局)
fun2: () => {
console.log(this.name); // undefined
}
};
obj.fun1();
obj.fun2();arguments 类数组,接收所有实参...rest 替代// 普通函数
function f1() {
console.log(arguments); // [1,2,3]
}
f1(1,2,3);
// 箭头函数
const f2 = (...rest) => {
console.log(rest); // [1,2,3]
};
f2(1,2,3);new 实例化,充当构造函数// 普通函数可用 new
function Person() {}
const p1 = new Person();
// 箭头函数不能 new
const Student = () => {};
// const p2 = new Student(); // 报错prototype 显式原型,支持原型链继承function a(){}
console.log(a.prototype); // {constructor: ...}
const b = () => {};
console.log(b.prototype); // undefinedcall/apply/bind 强行修改 thisthis 早已确定,修改无效const test = () => {
console.log(this);
};
// 强行绑定 this 也不生效
test.call({ name: "张三" }); // 依旧指向外层 this* 成为生成器 function*,配合 yieldyield,不能当生成器使用原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。