Literal Syntax Feature Analysis 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: info-contact@alibabacloud.com 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.