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