JavaScript Object literal

Source: Internet
Author: User

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. The following are examples of JavaScript literal values, such as 5, true, false, and null. They represent an integer, two boolean values, and an empty object respectively.
JavaScript also supports object and array literal, allowing you to use a simple and readable method to create arrays and objects. Consider the following statement. An object (firstName and lastName) containing two attributes is created ):

 

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

 

The right side of the preceding value assignment statement is an 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. To create an Array, you can create an instance of the Array object:

 

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

 

The preceding example shows that the object and array literal can contain other literal values. The following is a more complex example:

 

The object assigned to the team variable has three attributes: name, members, and count. Note: ''indicates an empty string. [] is an empty array. Even the value of the count attribute is a literal, that is, function literal ):

 

The literal expression of a function is constructed as follows: the first is a function keyword, followed by a function name (optional) and a parameter table. Then the function body is enclosed in braces.

The preceding section describes the literal content. Next we will introduce the JavaScript Object logging method (JavaScript Object Notation, JSON), which is a method used to describe files and arrays, it consists of a subset of the JavaScript literal. JSON is becoming more and more popular among Ajax developers because it can be used to exchange data and often replaces XML.

========================================================== ======================================
JavaScript Object literal examples (original)

Object literal:

// Only static attributes and methods can be added
Var myObject = {
PropertyA: sha,
PropertyB: feng,
MethodA: function (){
Alert (this. propertyA ++ this. propertyB );
},
MethodB: function (){}
}

MyObject. methodA ();


// You can use the prototype attribute to add public attributes and methods.

Function myConstructor2 () {}; // declare the constructor. You can use the object literal syntax to add all public members to the prototype attribute.

Myconstructor2.prototype = {
Propertya: Sha,
Propertyb: Feng,
Methoda: function (){
Alert (this. propertya ++ this. propertyb );
},
Methodb: function (){}
}

VaR myconstrustor = new myconstructor2 (); // declare an object
Myconstrustor. methoda ();

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.