Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wangsiyuan0215 opened this issue Apr 2, 2019 · 0 comments
Open

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

wangsiyuan0215 opened this issue Apr 2, 2019 · 0 comments
Labels

Comments

@wangsiyuan0215
Copy link
Owner

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

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

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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant