问题描述
我正在查看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');
}
};
推荐答案
阻止范围是其。如果您查看您会发现,此函数是根据 __ DEV __
的值有条件定义的,该值在转换过程中已优化,因为它等效于。
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');
}
};
}
这篇关于“ {”的含义是什么? "}"大括号围绕此反应库代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!