Skip to content

《详解箭头函数和普通函数的区别》 #12

@wangsiyuan0215

Description

@wangsiyuan0215

《详解箭头函数和普通函数的区别》

这篇文章主要用于知识点扫盲,箭头函数与普通函数的区别,盲点如下:

1. 箭头函数不存在 prototype,也就没有 this 的概念;

2. 箭头函数没有 constructor 构造函数,因此不能使用 new 关键字;

3. 箭头函数的 this 指向普通函数时,其 argument 继承与该普通函数;

4. 箭头函数的 this 指向全局时,使用 argument 时会报错;

5. 箭头函数不支持 new.target

new.target是ES6新引入的属性,普通函数如果通过new调用,new.target会返回该函数的引用。

此属性用于确定构造函数是否为 new 调用的;

  • 箭头函数的this指向全局对象,在箭头函数中使用箭头函数会报错
let a = () => {
	console.log(new.target); // 报错:new.target 不允许在这里使用
};
a();
  • 箭头函数的this指向普通函数,它的new.target就是指向该普通函数的引用。
new bb();
function bb() {
  let a = () => {
    console.log(new.target); // 指向函数bb:function bb(){...}
  };
  a();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions