In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use curly braces in javascript". In daily operation, I believe many people have doubts about how to use curly braces in javascript. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use braces in javascript". Next, please follow the editor to study!
The use of curly braces in JavaScript: 1, used to organize compound statements; 2, used to define an object, in most cases there should be pairs of attributes and values; 3, used to declare a function or function direct amount; 4, used as a syntax symbol for structured exception handling.
The operating environment of this tutorial: windows10 system, javascript1.8.5 version, Dell G3 computer.
What is the use of curly braces in javascript
Curly braces "{}" in Javascript have four semantic functions:
Semantic 1. Organize compound statements, which are the most common:
If (condition) {/ /...} else {/ /...} for () {/ /...}
Semantic 2, object direct quantity declaration:
Var obj = {name: 'jack', age: 23}
The whole is an assignment statement, where {name:'jack',age:23} is an expression.
Semantic 3, declare a function or function direct quantity:
Function F1 () {/ /...} var f2 = function () {/ /...}
The difference between F1 and non-f2 is that the former is in the grammatical interpretation period and the latter is in the running period. The difference is that if the code calling the function is after the function definition, there is no difference; if the code calling the function is before the function definition, F1 can still be called, and f2 will report an error, indicating that f2 is not defined.
Semantics 4, syntax symbols for structured exception handling:
Try {/ /...} catch (ex) {/ /...} finally {/ /...}
There is a difference between the curly braces here and the matching statement (semantic 1). If there is only one statement in the curly braces, the curly braces can be omitted in if/else/for, but try/catch/finally cannot be omitted.
The following code has been entangled for a long time:
The function () {} () / anonymous function executes immediately, and the parsing period reports {}. Constructor / / gets the constructor of the direct quantity of the object, and reports an error during the parsing period
What is puzzling is why [] .constructor does not report an error. One is a constructor that wants to get the direct quantity of an object, and the other is a constructor that gets the direct quantity of an array.
Of course, adding a variable to receive will not report an error, var c = {}. Constructor
The same situation, such as var fn = function () {} (), will not report an error.
In fact, it is the "sentence first" of js, that is, {} is understood as a compound sentence block (semantic 1) rather than the semantics of an object direct quantity (semantic 2) or a declaration function (semantic 3).
Function () {} (), curly braces are understood as compound statements, and naturally the syntax of the preceding function () declaration function is incomplete, resulting in an error in the parsing period. {} .constructor, curly braces are understood as compound statements, followed by a dot operator, and an error is naturally reported without a reasonable object before the dot operator.
The fix is well known: add a mandatory operator ()
(function () {}) (), (function () {}); / / forces it to be understood as a function (semantic 3), and "function ()" means that the function is executed, that is, it is executed immediately after declaration. ({}) .constructor / / ({}) forces curly braces to be understood as object direct quantities (semantic 2), "object .xx" represents members of the object, and naturally subsequent dot operators can be executed normally.
Function () {} (); the cause of the syntax error is independent of (); the function call operator.
The essential reason is that function () {} is an effective grouping of token according to the lexical analysis period. Function is seen as the first token element in the start location of this ExpressionStatement. This is not allowed by EMCA262. The reason for not allowing it is clear, that is, it is afraid of causing ambiguity in function expressions and function declarations. You can understand that the function keyword can never be in the first place of an ExpressionStaement.
Let's look at the assignment statement f = function () {}
F: LeftHandSideExpression=: AssignmentOperator
Function () {}; is now considered to be the assignment expression part of the entire statement, namely AssignmentExpression. So he passed the syntax check reasonably and legally and became a function expression. FunctionExpression.
So at this point, even if you f = function () {} (); it's grammatically legal.
At this point, the study on "how to use curly braces in javascript" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.