0
var emptyObject = {};
{
  Object.freeze(emptyObject);
}

Above is a snippet from react library umd/react.development.js file. I am trying to understand why they are including the Object.freeze line inside these curly braces {}? I have never seen something like this in all docs, I have read so far.

var warningWithoutStack = function () {};

{
  warningWithoutStack = function (condition, format) {
    for (var _len = arguments.length, args = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
      args[_key - 2] = arguments[_key];
    }

    if (format === undefined) {
      throw new Error('`warningWithoutStack(condition, format, ...args)` requires a warning ' + 'message argument');
    }
    if (args.length > 8) {
      // Check before the condition to catch violations early.
      throw new Error('warningWithoutStack() currently supports at most 8 arguments.');
    }
    if (condition) {
      return;
    }
    if (typeof console !== 'undefined') {
      var argsWithFormat = args.map(function (item) {
        return '' + item;
      });
      argsWithFormat.unshift('Warning: ' + format);

      // We intentionally don't use spread (or .apply) directly because it
      // breaks IE9: https://github.com/facebook/react/issues/13610
      Function.prototype.apply.call(console.error, console, argsWithFormat);
    }
    try {
      // --- Welcome to debugging React ---
      // This error was thrown as a convenience so that you can use this stack
      // to find the callsite that caused this warning to fire.
      var argIndex = 0;
      var message = 'Warning: ' + format.replace(/%s/g, function () {
        return args[argIndex++];
      });
      throw new Error(message);
    } catch (x) {}
  };
}
Rehan Umar
  • 179
  • 1
  • 12
  • 2
    They serve no purpose at all in that code, see the answers to the linked question for details. (Now, if that code had included `let`, `const`, or `class` within the block that `Object.freeze` is in, the block would serve a purpose, because `let`, `const`, and `class` are block-scoped.) – T.J. Crowder Aug 06 '19 at 12:13
  • Looks like it's purely cosmetic, to indicate that this call is the object's "initialization". EDIT: just guessing, it's no widespread convention that I know of, more some kind of personal style. What I can say is they are devoid of any effect. – hugo Aug 06 '19 at 12:13
  • @Vencovsky it's imposible for me to include all code here. because these lines are in there top level factory function. So there entire code file should be include here. But I have seen this pattern of using curly braces in a lot places in there code. – Rehan Umar Aug 06 '19 at 12:14
  • @hugo I've heard/seen/read many definitions of code styling, but "cosmetic" beats them all. – briosheje Aug 06 '19 at 12:14
  • @T.J.Crowder This makes sense. But apart from these reasons can you think of any other reason for including these braces. Because their library is cluttered with these braces and I believe they do have a good reason for including them. In this question someone was referrring to the fact that they are might be to help collapse the code. But I can't get my head around why `{...}` this would be helpful in code rather then `var emptyObject = {...}`https://stackoverflow.com/questions/18130025/javascript-curly-braces-with-no-function-or-json – Rehan Umar Aug 06 '19 at 12:23
  • I am editing the question to include another code snippet from react library code. – Rehan Umar Aug 06 '19 at 12:24
  • @RehanUmar - There are multiple answers to the linked question showing ways blocks *can* be used. One of them is probably the reason the library author is doing this, perhaps [the IDE code folding answer](https://stackoverflow.com/a/18130388/157247). – T.J. Crowder Aug 06 '19 at 12:31
  • @T.J.Crowder Yeah, I liked shmiddty answer. https://stackoverflow.com/a/18130388/4405934 – Rehan Umar Aug 06 '19 at 12:40

0 Answers0