Literal syntax in Javascript

Source: Internet
Author: User

When I first started learning JavaScript, I was always confused by some inexplicable syntax forms in JavaScript. I don't know how many inexplicable syntax forms it supports? Now, through a few months of in-depth understanding, we have made them clearer. So let's talk about the literal syntax feature of Js.

What are the features of regionalization in JavaScript? My God, which language does not use text? What can I do without text? Use stream of consciousness? Faint.

Although each of our languages uses text representation, it is recommended that ASM be far away from text, while basic is text, c/C ++, C #, and Java are all texts. This is correct. Their language expressions are in the form of text, but they cannot use text to represent all content. Simple types such as numbers and strings are okay, but can C # or Java use text to represent an object instance (instead of using a bunch of definition statements and then new object instances )? Obviously, JavaScript does not work. However, JavaScript provides a fully written method for all data types, including complex objects.

Common JS data types include number, Boolean, String, array, function, and object. Here, the number, Boolean, and string types are simple, and text writing is their basic method. If you use new XXX () to define them, you will think it is a fart.

Number: VaR I =   100 ; I =   100.11 ;
Boolean: VaR B =   True ; B =   False ;
String: VaR Str = ' This Is a string .';

What should we do for complex data types, functions, arrays, and objects? Functions are defined in text. Let's take a look at how arrays and objects are represented. Suppose we have an array:

VaR Ary =   New Array ( 6 );
Ary [ 0 ] =   Null ;
Ary [ 1 ] =   1 ;
Ary [ 2 ] = 'String ';
Ary [ 3 ] =   True ;
Ary [ 4 ] =   Function ()
Return 'Kekeke ';
Ary [ 5 ] =   New Myobject ();

We use the text method (that is, the initialization method we usually call) to write this array. It will be:

VaR Ary1 = [ Null , 1 , 'String ', True , Function (){ Return 'Kekeke ';}, New Myobject ()];

Is it much simpler than above? In addition, the array regionalization method can be far more complex than this method, for example:

VaR ary2 = []; // empty array, equivalent to new array ();
VaR ary3 = [ 1 ,[ 2 ,[ 3 ,[ 4 ,[ 5 ,[ 6 ,[ 7 ,[ 8 ,[ 9 ,[ 0 ];

What is the third ary3 array? I don't know @_@.

No, why is ary [5] New myobject? Oh, sorry, let's take the myobject example again, if it is defined:

Function Myobject ()
This . Properties1 =   1 ;
This . Properties2 = ' 2 ';
This . Properties3 = [ 3 ];
This . Tostring =   Function ()
Return '[Class myobject]';

Myobject. Prototype. Method1 =   Function ()
Return   This . Properties1 +   This . Properties3 [ 0 ];

Myobject. Prototype. method2 =   Function ()
Return   This . Properties2;

So how can we implement var OBJ = new myobject? In fact, it is quite simple. The regionalization definition of obj is as follows:

VaR OBJ =  
Properties1: 1 , Properties2 :' 2 ', Properties3 :[ 3 ],
Method1: Function (){ Return   This . Properties1 +   This . Properties3 [ 0 ] ;},
Method2: Function (){ Return   This . Preperties2 ;}

Although the direct regionalization definition of this class instance is not simplified, It is not bad. In this way, we can replace the New myobject () in ary with this regionalization class instance. The syntax defined by class instance regionalization is to use a pair of "{}" to represent the class, that is, "{}" is equivalent to "new object ()". Then "{}" organizes attributes and methods by "key: value", the key can be any [A-Za-z0-9 _] character combination, or even the beginning of the number is legal @_@, value is any legal regionalized JavaScript data, and each key-value pair is separated.

Related Article

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: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.