Ambiguity of curly braces ' {} ' in JavaScript

Source: Internet
Author: User
Tags constructor exception handling function definition

      This article mainly introduces the ambiguity of curly braces "{}" in JavaScript. Need friends can come to the reference, I hope to help you.

JS in the curly braces have four semantic functions   semantics 1, organization compound statement, this is the most common       code as follows: if (condition) {   //... }else {   //... }  for () {   //... }    Semantics 2, Object Direct Volume declaration   code as follows: var obj = {  &nbs P   Name: ' Jack ',      age:23 };    Whole is an assignment statement, where {name: ' Jack ', age:23} is an expression.   Semantics 3, Declare function or function Direct quantity       code as follows: Function F1 () {     //... }    var F2 = Fun Ction () {     //... }    F1 differs from non F2 in that the former is in the grammar interpretation period and the latter is in the operating period. The difference is that if the code that calls the function has no difference after the function is defined, if the code calling the function is still callable before the function definition, F2 will F1 the error, prompting F2 undefined.   Semantics 4, syntactic notation for structured exception handling         code is as follows: Try {     //... }catch (ex) {    & nbsp ... }finally{     //... }    Here the braces are different from the conforming statement (Semantic 1), if there is only one statement in the curly braces, in the if/else/ For, and so on, curly braces can be omitted, but try/catch/finally cannot be omitted.   The following code tangled up with me     code as follows: function () {} ()//anonymous function executes immediately, parsing period   {}.coNstructor//Get the object direct quantity of the constructor, parsing period error     puzzling is why [].constructor so write but not the error, one is to get the object direct quantity of the constructor, one is to get the array of direct quantity of the constructor.   Of course add a variable receive also will not error     var c = {}.constructor;   The same situation, such as     var fn = function () {} (), does not complain.   is actually JS's "statement first" in the mischief, that is, {} is understood as a compound statement block (semantic 1) rather than object Direct (semantic 2) or declarative function (semantic 3) semantics.   function () {} (), curly braces are understood as compound statements, and the previous function () declares that the syntax of functions is incomplete, resulting in an error in the parsing period.   {}.constructor, curly braces are understood as compound statements, followed by the dot operator, the point operator is not a reasonable object naturally also error.   Repair methods are well known: Add a force operator () (function () {}) (), (function () {});//force it to be understood as a function (semantic 3), and "function ()" means execute the function, which is executed immediately after the declaration.   ({}). constructor//({}) forces the braces to be understood as Object Direct (semantic 2), and "object. xx" means to get the members of the object, and the dot operator behind it can be performed normally.    
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.