JavaScript Object Literal Explanation _ Basics

Source: Internet
Author: User
Tags arrays

In programming languages, literal is a notation for representing values. For example, "Hello, world!" represents a string literal (string literal) in many languages, and JavaScript is no exception. The following are examples of JavaScript literals, such as 5, true, false, and NULL, which represent an integer, two Boolean values, and an empty object, respectively.

JavaScript also supports objects and array literals, allowing you to create arrays and objects using a concise and readable notation Falay. Consider the following statement, which creates an object that contains two properties (FirstName and LastName):

You can also create the same object by using an equivalent method:

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

The preferred approach, however, is to use an array literal (array literal), which is a comma-separated list of values, enclosed in brackets:

The previous example shows that the object and array literals can contain other literal quantities. The following is a more complex example:

The object assigned to the team variable has 3 properties: Name, members, and count. Note that "represents 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 functional name (optional), and a parameter table. The function body is then enclosed in curly braces.

As described above, the following is a description of the JavaScript Object notation (JavaScript objects Notation,json), a notation for describing files and arrays, consisting of a subset of JavaScript literal quantities. JSON is becoming more and more popular among AJAX developers because this format can be used to exchange data, often replacing XML.

=========================================================================
Examples of JavaScript object literals (original)
Object literal amount:

Only static properties and methods can be added
var myobject={
 Propertya:sha,
 Propertyb:feng,
 methoda:function () {
 alert ( this.propertya+ +this.propertyb);
 },
 methodb:function () {}
}

Myobject.methoda ();


The prototype property enables you to add public and method

function MyConstructor2 () {};//Declaration constructors, which can be used to add all public members

to the prototype property by using object literal syntax myconstructor2.prototype={
 Propertya:sha,
 Propertyb:feng,
 methoda:function () {
 alert ( this.propertya+ +this.propertyb);
 },
 methodb:function () {}
}

var myconstrustor=new MyConstructor2 (); Declaration Object
Myconstrustor.methoda ();

The above is the JavaScript object literal explanation All content, hoped can give everybody a reference, also hoped that everybody supports the cloud habitat community.

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.