What is the significance of “{” “}” braces around this react library code?
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');
}
};
javascript reactjs ecmascript-6
|
show 2 more comments
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');
}
};
javascript reactjs ecmascript-6
2
Can you cite this source? They seem superfluous, but my guess is you've "dumbed down the code" for the sake of presentation and just making them appear superfluous.
– Patrick Roberts
2 hours ago
The braces don't really do much, they just give the sense of clarity. Having it that way makes it unmissable. It tries to create a block scope there, which is something that isn't available in JS (though possible withlet
andconst
).
– John Kennedy
2 hours ago
1
It creates a block around the function. But since there's no block scope when using vars, it has no meaning written this way. Possible explanations that come to mind are: 1) Transpiled code, 2) Inexperienced dev, 3) There used to be a label on the block that got removed.
– Shilly
2 hours ago
@Shillysince there's no block scope in JS
is wrong. Starting in ES6let
andconst
are block scope declarations.
– Patrick Roberts
2 hours ago
1
@abhishekgangwar there's like 100s of files in here. Save us some time and edit your question to properly cite this source.
– Patrick Roberts
2 hours ago
|
show 2 more comments
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');
}
};
javascript reactjs ecmascript-6
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');
}
};
javascript reactjs ecmascript-6
javascript reactjs ecmascript-6
edited 2 hours ago
John Kennedy
2,5102926
2,5102926
asked 2 hours ago
abhishek gangwar
511312
511312
2
Can you cite this source? They seem superfluous, but my guess is you've "dumbed down the code" for the sake of presentation and just making them appear superfluous.
– Patrick Roberts
2 hours ago
The braces don't really do much, they just give the sense of clarity. Having it that way makes it unmissable. It tries to create a block scope there, which is something that isn't available in JS (though possible withlet
andconst
).
– John Kennedy
2 hours ago
1
It creates a block around the function. But since there's no block scope when using vars, it has no meaning written this way. Possible explanations that come to mind are: 1) Transpiled code, 2) Inexperienced dev, 3) There used to be a label on the block that got removed.
– Shilly
2 hours ago
@Shillysince there's no block scope in JS
is wrong. Starting in ES6let
andconst
are block scope declarations.
– Patrick Roberts
2 hours ago
1
@abhishekgangwar there's like 100s of files in here. Save us some time and edit your question to properly cite this source.
– Patrick Roberts
2 hours ago
|
show 2 more comments
2
Can you cite this source? They seem superfluous, but my guess is you've "dumbed down the code" for the sake of presentation and just making them appear superfluous.
– Patrick Roberts
2 hours ago
The braces don't really do much, they just give the sense of clarity. Having it that way makes it unmissable. It tries to create a block scope there, which is something that isn't available in JS (though possible withlet
andconst
).
– John Kennedy
2 hours ago
1
It creates a block around the function. But since there's no block scope when using vars, it has no meaning written this way. Possible explanations that come to mind are: 1) Transpiled code, 2) Inexperienced dev, 3) There used to be a label on the block that got removed.
– Shilly
2 hours ago
@Shillysince there's no block scope in JS
is wrong. Starting in ES6let
andconst
are block scope declarations.
– Patrick Roberts
2 hours ago
1
@abhishekgangwar there's like 100s of files in here. Save us some time and edit your question to properly cite this source.
– Patrick Roberts
2 hours ago
2
2
Can you cite this source? They seem superfluous, but my guess is you've "dumbed down the code" for the sake of presentation and just making them appear superfluous.
– Patrick Roberts
2 hours ago
Can you cite this source? They seem superfluous, but my guess is you've "dumbed down the code" for the sake of presentation and just making them appear superfluous.
– Patrick Roberts
2 hours ago
The braces don't really do much, they just give the sense of clarity. Having it that way makes it unmissable. It tries to create a block scope there, which is something that isn't available in JS (though possible with
let
and const
).– John Kennedy
2 hours ago
The braces don't really do much, they just give the sense of clarity. Having it that way makes it unmissable. It tries to create a block scope there, which is something that isn't available in JS (though possible with
let
and const
).– John Kennedy
2 hours ago
1
1
It creates a block around the function. But since there's no block scope when using vars, it has no meaning written this way. Possible explanations that come to mind are: 1) Transpiled code, 2) Inexperienced dev, 3) There used to be a label on the block that got removed.
– Shilly
2 hours ago
It creates a block around the function. But since there's no block scope when using vars, it has no meaning written this way. Possible explanations that come to mind are: 1) Transpiled code, 2) Inexperienced dev, 3) There used to be a label on the block that got removed.
– Shilly
2 hours ago
@Shilly
since there's no block scope in JS
is wrong. Starting in ES6 let
and const
are block scope declarations.– Patrick Roberts
2 hours ago
@Shilly
since there's no block scope in JS
is wrong. Starting in ES6 let
and const
are block scope declarations.– Patrick Roberts
2 hours ago
1
1
@abhishekgangwar there's like 100s of files in here. Save us some time and edit your question to properly cite this source.
– Patrick Roberts
2 hours ago
@abhishekgangwar there's like 100s of files in here. Save us some time and edit your question to properly cite this source.
– Patrick Roberts
2 hours ago
|
show 2 more comments
4 Answers
4
active
oldest
votes
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');
}
};
}
1
Yes, the problem here is that the code was misquoted.
– estus
2 hours ago
add a comment |
This code makes perfect sense.
var v = function () {};
{
v = function (format) {
// actual code
};
}
The first assignment defines v
to an empty placeholder function so that code does not break.
The second assignment contains the actual code of the function. It is inside a block scope, which is a legit JS construct.
But... block scoping does nothing because of variable hoisting, which nullifies any locality contrary to C/C++ adepts' expectations. Many say there is no block scoping in JS, which is false. There is block scoping but it's ineffective (apart from more recent let
/const
declarations).
So what this code does is abuse the ineffective block syntax to separate visually parts of code.
But (and this is what I think is going on here) what we see here is just an EXAMPLE. I could very well come up with another example that makes perfect sense, such as this:
var v = function () {};
{
let localValue = 0;
v = function (format) {
// actual code using localValue
localValue = 1;
};
}
In other words, you may find other examples in the code base which leverages block scoping through let
/const
and encapsulates the definition as shown. The example you give just doesn't leverage this opportunity but the scoping remains because:
- it does not disrupt or break code;
- uniformity;
- in future may carry more weight adding
let
/const
.
This is all guessing on my part.
add a comment |
Curly braces here are block statement. They don't serve any good purpose in this case and can safely omitted. Block scope doesn't work with var
. Even if it was let
, block statement wouldn't affect it because validateFormat
is already defined outside block statement.
An example where block statement is useful would be:
let validateFormat = function () {};
{
// doesn't reassign validateFormat from outer scope
let validateFormat = function (format) {
if (format === undefined) {
throw new Error('invariant requires an error message argument');
}
};
}
add a comment |
I don't think that there is any sense behind these brackets. In some scenarios, such brackets may have a use when used with let
or const
variables, however, as mentioned, in this snippet it is useless.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53941926%2fwhat-is-the-significance-of-braces-around-this-react-library-code%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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');
}
};
}
1
Yes, the problem here is that the code was misquoted.
– estus
2 hours ago
add a comment |
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');
}
};
}
1
Yes, the problem here is that the code was misquoted.
– estus
2 hours ago
add a comment |
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');
}
};
}
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');
}
};
}
edited 2 hours ago
answered 2 hours ago
Patrick Roberts
19.1k33373
19.1k33373
1
Yes, the problem here is that the code was misquoted.
– estus
2 hours ago
add a comment |
1
Yes, the problem here is that the code was misquoted.
– estus
2 hours ago
1
1
Yes, the problem here is that the code was misquoted.
– estus
2 hours ago
Yes, the problem here is that the code was misquoted.
– estus
2 hours ago
add a comment |
This code makes perfect sense.
var v = function () {};
{
v = function (format) {
// actual code
};
}
The first assignment defines v
to an empty placeholder function so that code does not break.
The second assignment contains the actual code of the function. It is inside a block scope, which is a legit JS construct.
But... block scoping does nothing because of variable hoisting, which nullifies any locality contrary to C/C++ adepts' expectations. Many say there is no block scoping in JS, which is false. There is block scoping but it's ineffective (apart from more recent let
/const
declarations).
So what this code does is abuse the ineffective block syntax to separate visually parts of code.
But (and this is what I think is going on here) what we see here is just an EXAMPLE. I could very well come up with another example that makes perfect sense, such as this:
var v = function () {};
{
let localValue = 0;
v = function (format) {
// actual code using localValue
localValue = 1;
};
}
In other words, you may find other examples in the code base which leverages block scoping through let
/const
and encapsulates the definition as shown. The example you give just doesn't leverage this opportunity but the scoping remains because:
- it does not disrupt or break code;
- uniformity;
- in future may carry more weight adding
let
/const
.
This is all guessing on my part.
add a comment |
This code makes perfect sense.
var v = function () {};
{
v = function (format) {
// actual code
};
}
The first assignment defines v
to an empty placeholder function so that code does not break.
The second assignment contains the actual code of the function. It is inside a block scope, which is a legit JS construct.
But... block scoping does nothing because of variable hoisting, which nullifies any locality contrary to C/C++ adepts' expectations. Many say there is no block scoping in JS, which is false. There is block scoping but it's ineffective (apart from more recent let
/const
declarations).
So what this code does is abuse the ineffective block syntax to separate visually parts of code.
But (and this is what I think is going on here) what we see here is just an EXAMPLE. I could very well come up with another example that makes perfect sense, such as this:
var v = function () {};
{
let localValue = 0;
v = function (format) {
// actual code using localValue
localValue = 1;
};
}
In other words, you may find other examples in the code base which leverages block scoping through let
/const
and encapsulates the definition as shown. The example you give just doesn't leverage this opportunity but the scoping remains because:
- it does not disrupt or break code;
- uniformity;
- in future may carry more weight adding
let
/const
.
This is all guessing on my part.
add a comment |
This code makes perfect sense.
var v = function () {};
{
v = function (format) {
// actual code
};
}
The first assignment defines v
to an empty placeholder function so that code does not break.
The second assignment contains the actual code of the function. It is inside a block scope, which is a legit JS construct.
But... block scoping does nothing because of variable hoisting, which nullifies any locality contrary to C/C++ adepts' expectations. Many say there is no block scoping in JS, which is false. There is block scoping but it's ineffective (apart from more recent let
/const
declarations).
So what this code does is abuse the ineffective block syntax to separate visually parts of code.
But (and this is what I think is going on here) what we see here is just an EXAMPLE. I could very well come up with another example that makes perfect sense, such as this:
var v = function () {};
{
let localValue = 0;
v = function (format) {
// actual code using localValue
localValue = 1;
};
}
In other words, you may find other examples in the code base which leverages block scoping through let
/const
and encapsulates the definition as shown. The example you give just doesn't leverage this opportunity but the scoping remains because:
- it does not disrupt or break code;
- uniformity;
- in future may carry more weight adding
let
/const
.
This is all guessing on my part.
This code makes perfect sense.
var v = function () {};
{
v = function (format) {
// actual code
};
}
The first assignment defines v
to an empty placeholder function so that code does not break.
The second assignment contains the actual code of the function. It is inside a block scope, which is a legit JS construct.
But... block scoping does nothing because of variable hoisting, which nullifies any locality contrary to C/C++ adepts' expectations. Many say there is no block scoping in JS, which is false. There is block scoping but it's ineffective (apart from more recent let
/const
declarations).
So what this code does is abuse the ineffective block syntax to separate visually parts of code.
But (and this is what I think is going on here) what we see here is just an EXAMPLE. I could very well come up with another example that makes perfect sense, such as this:
var v = function () {};
{
let localValue = 0;
v = function (format) {
// actual code using localValue
localValue = 1;
};
}
In other words, you may find other examples in the code base which leverages block scoping through let
/const
and encapsulates the definition as shown. The example you give just doesn't leverage this opportunity but the scoping remains because:
- it does not disrupt or break code;
- uniformity;
- in future may carry more weight adding
let
/const
.
This is all guessing on my part.
answered 2 hours ago
pid
9,48352346
9,48352346
add a comment |
add a comment |
Curly braces here are block statement. They don't serve any good purpose in this case and can safely omitted. Block scope doesn't work with var
. Even if it was let
, block statement wouldn't affect it because validateFormat
is already defined outside block statement.
An example where block statement is useful would be:
let validateFormat = function () {};
{
// doesn't reassign validateFormat from outer scope
let validateFormat = function (format) {
if (format === undefined) {
throw new Error('invariant requires an error message argument');
}
};
}
add a comment |
Curly braces here are block statement. They don't serve any good purpose in this case and can safely omitted. Block scope doesn't work with var
. Even if it was let
, block statement wouldn't affect it because validateFormat
is already defined outside block statement.
An example where block statement is useful would be:
let validateFormat = function () {};
{
// doesn't reassign validateFormat from outer scope
let validateFormat = function (format) {
if (format === undefined) {
throw new Error('invariant requires an error message argument');
}
};
}
add a comment |
Curly braces here are block statement. They don't serve any good purpose in this case and can safely omitted. Block scope doesn't work with var
. Even if it was let
, block statement wouldn't affect it because validateFormat
is already defined outside block statement.
An example where block statement is useful would be:
let validateFormat = function () {};
{
// doesn't reassign validateFormat from outer scope
let validateFormat = function (format) {
if (format === undefined) {
throw new Error('invariant requires an error message argument');
}
};
}
Curly braces here are block statement. They don't serve any good purpose in this case and can safely omitted. Block scope doesn't work with var
. Even if it was let
, block statement wouldn't affect it because validateFormat
is already defined outside block statement.
An example where block statement is useful would be:
let validateFormat = function () {};
{
// doesn't reassign validateFormat from outer scope
let validateFormat = function (format) {
if (format === undefined) {
throw new Error('invariant requires an error message argument');
}
};
}
answered 2 hours ago
estus
66.9k2198210
66.9k2198210
add a comment |
add a comment |
I don't think that there is any sense behind these brackets. In some scenarios, such brackets may have a use when used with let
or const
variables, however, as mentioned, in this snippet it is useless.
add a comment |
I don't think that there is any sense behind these brackets. In some scenarios, such brackets may have a use when used with let
or const
variables, however, as mentioned, in this snippet it is useless.
add a comment |
I don't think that there is any sense behind these brackets. In some scenarios, such brackets may have a use when used with let
or const
variables, however, as mentioned, in this snippet it is useless.
I don't think that there is any sense behind these brackets. In some scenarios, such brackets may have a use when used with let
or const
variables, however, as mentioned, in this snippet it is useless.
answered 2 hours ago
sermonion x
588
588
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53941926%2fwhat-is-the-significance-of-braces-around-this-react-library-code%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Can you cite this source? They seem superfluous, but my guess is you've "dumbed down the code" for the sake of presentation and just making them appear superfluous.
– Patrick Roberts
2 hours ago
The braces don't really do much, they just give the sense of clarity. Having it that way makes it unmissable. It tries to create a block scope there, which is something that isn't available in JS (though possible with
let
andconst
).– John Kennedy
2 hours ago
1
It creates a block around the function. But since there's no block scope when using vars, it has no meaning written this way. Possible explanations that come to mind are: 1) Transpiled code, 2) Inexperienced dev, 3) There used to be a label on the block that got removed.
– Shilly
2 hours ago
@Shilly
since there's no block scope in JS
is wrong. Starting in ES6let
andconst
are block scope declarations.– Patrick Roberts
2 hours ago
1
@abhishekgangwar there's like 100s of files in here. Save us some time and edit your question to properly cite this source.
– Patrick Roberts
2 hours ago