JS Object literal

Source: Internet
Author: User

In a programming language, a literal is a notation that represents a value. For example, "Hello, world!" In many languages represents a string literal (string literal), and JavaScript is no exception. The following are also examples of JavaScript literals, such as 5, true, false, and NULL, which represent an integer, two booleans, and an empty object, respectively.
JavaScript also supports object and array literals, allowing the creation of arrays and objects using a concise and readable notation capable. Consider the following statement, which creates an object with two attributes (FirstName and LastName)

You can also use an equivalent method to create the same object:

The right side of the above assignment statement is an object literal (literal). The object literal is a list of name-value pairs, separated by commas between each name value pair and enclosed by a curly brace. Each name value pair represents an attribute of the object, separated by a colon between the name and the value. To create an array, you can create an instance of an array object:

However, the preferred method is to use an array literal (array literal), which is a comma-separated list of values, enclosed in brackets:

The previous example shows that objects and array literals can contain other literals. The following is a more complex example:

The object assigned to the team variable has 3 properties: Name, members, and count. Note that ' indicates an empty string, [] is an empty array. Even the value of the Count property is also a literal, that is, the function literal (functions literal):

The function literal is constructed as follows: A function keyword, followed by a name (optional) and a parameter table. The function body is then enclosed in curly braces. The above has introduced the literal, the following describes the JavaScript Object notation (JavaScript object Notation,json), which is a notation for describing the file and array, consisting of a subset of the JavaScript literal. JSON is becoming more popular among Ajax developers because this format can be used to exchange data, often in place of XML.

JS Object literal

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.