匿名函数与命名函数之间的差异作为对象键的值 [英] Difference between anonymous function vs named function as value for an object key

查看:119
本文介绍了匿名函数与命名函数之间的差异作为对象键的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Babel编译器将JSX编译为JavaScript。以下是我对此感兴趣的代码。

I have compiled JSX to JavaScript using Babel compiler. Here is the piece of code I'm intrigued with.

getInitialState: function getInitialState() {
//List out different states that ListComponent could possibly have
return {
  showList: true,
  listType: this.props.type

将JSX编译为JS后,getInitialState是一个命名方法getInitialState()。我无法弄清楚为什么它不是匿名方法。

After compiling JSX to JS, getInitialState is a named method getInitialState(). I couldn't figure out why it isn't a anonymous method.

原始代码:

getInitialState: function() {
//List out different states that ListComponent could possibly have
return {
  showList: true,
  listType: this.props.type

这样的写作是否有任何性能优势?

Is there any performance advantage writing like this?

推荐答案

由于文件大小,加载时间可能没有性能损失。

There is no performance hit, except maybe the load time because of the file size.

否则命名匿名函数有助于解决问题,因为这些名称出现在大多数浏览器的错误堆栈跟踪中。

Naming otherwise anonymous functions helps fixing issues since those names appear in the error stack traces in most browsers.

这个视频很好地解释了当您为匿名函数设置名称时会发生什么。

This video does a good job at explaining what happens when you set names to anonymous functions.

ECMA262中也包含此行为ES6语言规范。您可以查看这里

Also this behavious has been included in the ECMA262 ES6 language specification. You can check that here.

这篇关于匿名函数与命名函数之间的差异作为对象键的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆