JavaScript Instance Tutorial (19) array

Source: Internet
Author: User
Tags array arrays object constructor numeric value script tag
Javascript| Tutorials | arrays

Using JavaScript arrays

Only date objects and user-defined objects exist in the constructor in JavaScript 1.0. You might expect to have an array constructor, but you haven't been able to do it until JavaScript 1.1 appears, and your expectations have become reality. We can define user objects as follows:

function Blankarray (n) {

for (var i=0 i < n; i++)

This[i] = null;

This.length = n;

}

The Blankarray function creates an array with n blank entries in the array. Here's another example of using this constructor:

var myarray = new Blankarray (3);

Myarray[0] = "Hello";

MYARRAY[1] = "World";

MYARRAY[2] = "!";

If you have some experience with other programming languages (such as C), you will have to wait until the index of the array starts at 0 instead of starting at 1. So in the example above the array index is from 0 to 2, so the length of this array is 3.

The following code is a more advanced array builder. It uses the ' arguments ' property to assign values to the array instead of just creating an empty array element, which is present for all functions. Although no arguments are given, the values passed can still be accessed through the arguments array. The specific code is as follows:

function Makearray () {

for (Var i=0 i < arguments.length; i++)

This[i] = arguments[i];

This.length = Arguments.length;

}

The constructor's invocation can be like this:

var myarray = new Makearray ("Hello", "World", "!");

In JavaScript 1.1, creating an array constructor combines the specific Blankarray and Makearray. As in one of the following calls:

var myarray = new Array (3); Requires JS 1.1

This call creates a blank array (length 3) as the Blankarray constructor shown above. And in Netscape it's called ' dense array ' because each element has a numeric value that can be created as follows:

var myarray = new Array (value1, value2, value3); JS 1.1

Now that you are familiar with constructors and object properties, we can give some more examples. If you are using JavaScript 1.1 or later, you can use the array constructor. Otherwise you would be more appropriate to use the Makearray object. var workpeople = new Array (

New Person ("Thomas", "green"),

New Person ("Richard", "Blue"),

New Person ("Harold", "Chartreuse")

);

Workpeople is an array of three person objects. Each person has a name (first name), Age (Ages), colour (skin color), and birthyear (year of birth) attributes.

Here's another example to use for what we've learned above. It is also a reminder that the person code block and the Workpeople array must be included in the same script tag or elsewhere in the Web page.

<script language= "JavaScript" >

<!--Hide from older browsers


function Whoisoldest (Parray) {

var poldest = parray[0]; The first person in the array

for (Var i=1 i < parray.length; i++)//on each additional person loop

if (Parray[i].isolder (poldest))///If they are older

Poldest = Parray[i]; Set them to the oldest

return poldest;

}

var senior = Whoisoldest (workpeople);

document.write ("The oldest is" + Senior.name + "<br>" + senior);


Stop hiding-->

</SCRIPT>

The output from the example above is:

The oldest is Richard

Richard is born in 1963

and is years old.

The value returned from the Whoisoldest function is a person object with all the properties and methods of the object. This is why the name attribute and the variable Printperson () method (called senior) can be referenced.

Some other features of the Whoisoldest function are:

A. Handle any people array.

B. Handle an array of any object type, this array has a Isolder () method.

If you're not using JavaScript to switch to other types of programming languages, you'll have to specify that the function input is an array of people, but not in JavaScript. This is one of the advantages of JavaScript.




Related Article

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.