What is the significance of “{” “}” braces around this react library code?

  • A+
Category:Languages

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');     }   }; } 

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');    } }; 

 


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');     }   }; } 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: