Learning notes for JavaScript objects and arrays

Source: Internet
Author: User

What is an object

is actually a type, that is, a reference type. The value of an object is an instance of the reference type. A reference type in ECMAScript is a data type that is used to organize data and functionality together. It is also often called a class.

1.Object type
Use the new Object ();

Create with literal volume

The code is as follows Copy Code

var box = {

Name: ' Caibaojian.com ',

Age:24

}

Output

Alert (Box.name)

Alert (box[' name '));

var box = {

Run:function () {

Return ' 123 ';

}

}

alert (Box.run); Print out the entire function code

Alert (Box.run ()); Print return value

You can use the Delete output property www.111cn.net
Delete Box.name

In the actual development, we generally prefer the literal declaration way. Because it is clear, the syntax code is small, but also gives the person a kind of encapsulation feeling

Literal is also the preferred way for a function to pass a large number of optional arguments.

The code is as follows Copy Code

function box (obj) {

alert (obj.name);

if (obj.name!=undefined) alert ();
}

var obj = {

Name: ' Caibaojian.com ',

Age:24,

height:178

};

box (obj);

Two: Array type
In addition to the object type, the array type is the most commonly used type of ECMAScript. Also, the array type in the ECMAScript differs greatly from the arrays in other languages. Although arrays are ordered, each element of an array in ECMAScript can hold any type. The size can also be adjusted.

There are two different ways to create:

1. Using new

The code is as follows Copy Code

var box = new Array ();

var box = new Array (10);

var box = new Array (' Caibaojian ', 24, ' front-end development ', ' Zhongshan ');

Alert (typeof box); belongs to type Object

2. The above can omit new

3. Use literal methods to create

  code is as follows copy code

var box = [];

var  box = [' Caibaojian ',];

Var box =[];

box[0]= ' Caibaojian ';

Box[1] =;

Alert (box);

Box.length = 10;//force element amount

box[box.length]= ' Caibaojian ';

var box = [

{

Name: ' Caibaojian ',

age:24

},

[1,2,3,new Object ()],

' computer programming ',

25+25,

New Array (1,2,3)

;

Alert (box);

Alert (box[0].name);

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.