JavaScript mode (3): literal

Source: Internet
Author: User

What is literal?

In programming languages, a literal is a way to express a value. For example, "Hello, World! "In many languages, it represents a string literal (string literal), and JavaScript is no exception. For example, for example, 5, true, false, and null, they represent an integer, two boolean values, and an empty object.

At first glance, it may be difficult to define the number of words. In fact, my understanding is that what I can understand at a glance is the number of words. Object literal

An object literal is a list of name-value pairs. Each name-value pair is separated by a comma and enclosed in braces. Each name-Value Pair represents an attribute of an object. Names and values are separated by a colon. Example:

First
Second

Ps1: In the comment, I commented out define blank object, which is actually inaccurate. In JavaScript, no object is empty, even {} inherits some objects. prototype attributes and Methods

Ps2: a similar problem occurs in the subsequent constructor. this = {} this indicates that the null Object is not Object. prototype. For details, refer

Ps3: Previously, I used the delete operator to delete global variables. The same object attributes can also be deleted through delete.

Ps4: do not add a comma (,) after the last attribute. An error occurs in the IE browser. When I wrote the Tree component, this Bug was found for N years.

The last 1 P should not forget to end the (;) on the right of the braces (;)

Array literal

The representation of the array literal is similar to that of the object literal. The Code is as follows:

Array literal notation does not have much content: it is just a list of elements separated by commas (,) and the entire list is packaged in square brackets. You can specify any type of values for the array literal, including objects or other arrays. The array literal syntax is very simple, clear, and elegant.

Regular Expression literal

The Regular Expression in JavaScript is also an object, so we can also create a regular expression in a literal way. The example is as follows:

As shown in the above Code: regular expressions have clear syntax and short syntax. Therefore, the literal representation should be given priority when creating regular expressions.

For more information about the regular expression syntax, read the regular expression notes:

The literal values I listed are confusing. For strings, values, and boolean values... I will not introduce them one by one. (Stealing laziness)

I was going to put the literal volume and the constructor together, but I thought it was not appropriate. The constructor expanded a lot and it would be messy to put them together, so I split them up.

In case of any errors, please correct me and make progress together!

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.