“ {”的含义是什么? "}"大括号围绕此反应库代码? [英] What is the significance of "{" "}" braces around this react library code?

查看:1371
本文介绍了“ {”的含义是什么? "}"大括号围绕此反应库代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看React库代码。看完之后,我发现了一段特殊的代码,我无法理解它的重要性。有人可以帮忙吗?

I was going through react library code. After going through I found a special piece of code I am unable to understand its significance. Can someone help?

var validateFormat = function () {};

{
  validateFormat = function (format) {
    if (format === undefined) {
      throw new Error('invariant requires an error message argument');
    }
  };
}

为什么反应开发人员将validateFormat包装在花括号中?这样做有什么意义吗?

Here why react developer has wrapped the validateFormat into curly braces? is there any significance of doing this.

如果我执行以下操作,则其工作原理相同-

If I do the following it works the same -

var validateFormat = function () {};

validateFormat = function (format) {
   if (format === undefined) {
   throw new Error('invariant requires an error message argument');
   }
};


推荐答案

阻止范围是其 babel-preset-fbjs 。如果您查看 原始来源 您会发现,此函数是根据 __ DEV __ 的值有条件定义的,该值在转换过程中已优化,因为它等效于 process.env.NODE_ENV!=='生产'

The block scope is a result of their babel-preset-fbjs. If you look at the original source you'll find that instead, this function is conditionally defined depending on the value of __DEV__, which is optimized out during transpilation since it is equivalent to process.env.NODE_ENV !== 'production'.

let validateFormat = () => {};

if (__DEV__) {
  validateFormat = function(format) {
    if (format === undefined) {
      throw new Error('invariant requires an error message argument');
    }
  };
}

这篇关于“ {”的含义是什么? "}"大括号围绕此反应库代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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