react-native javascript 中的 () => {} 和 function() {} 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37796654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between () => {} and function() {} in react-native javascript?
提问by Parth Tiwari
I have seen some functions defined as function(){}
and some functions defined as () => {}
.
我见过一些定义为的function(){}
函数和一些定义为() => {}
.
Is this related to Javascript version ES6?
这与 Javascript 版本 ES6 相关吗?
Also, how does use of this
keyword change from one function definition to another?
此外,this
关键字的使用如何从一个函数定义更改为另一个函数定义?
回答by cyberbit
The () => {}
is called an arrow function. They are, as you said, part of ES6. From the linked page:
在() => {}
被称为箭头功能。正如你所说,它们是 ES6 的一部分。从链接页面:
An arrow function expression has a shorter syntax compared to function expressions and lexically binds the
this
value (does not bind its ownthis
,arguments
,super
, ornew.target
). Arrow functions are always anonymous.
相比函数表达式的箭头函数表达式具有较短的语法和词汇结合
this
值(不结合其自身的this
,arguments
,super
,或new.target
)。箭头函数总是匿名的。