Introduction to objects and arrays in JavaScript BASICS (3)

Source: Internet
Author: User

Javascript: Object
Objects we have briefly introduced before. They are a type of object that aggregates multiple data values in one unit and uses names for access. They are a disordered set of attributes.
1. Several ways to create objects
Copy codeThe Code is as follows:
Var empty ={} // create an object without attributes.
Var person = {name: "ben", age: 22, sex: 'male'} // create an object using a direct volume
Var people = {name: 'Frank ', age: 21}, {name: 'Mary', age: 21}, sex: 'Man '} // The object element can be an object

2. Object Attributes
Copy codeThe Code is as follows:
Var person = {}; // create an object
Person. name = "Frank"; // Add attributes
Person. country = "china ";
Person. age = 22;
Person. american = new Object (); // This property is an Object.
Person. american. name = "Lisa ";
Person. american. country = "American ";
Person. american. age = 20;
Function displayperson (personmore) // print the above object
{
For (var p in personmore) // enumeration Loop
{
If (typeof (personmore [p]) = "object") // determine the type
{
For (var o in personmore [p])
{
Document. write ("American people:" + o + "\ t" + personmore [p] [o] + "<br/> ");
}
Document. write ("<br/> ");
Continue; // end this loop and perform the next loop.
Document. write ("china people:" + p + "\ t" + personmore [p] + "<br/> ");
}
}
Displayperson (person); // call a function
// Output china people: name Frank
// China people: country china
// China people: age 22
// American people: name Lisa
// American people: country American
// American people: age 20

3. Delete attributes
Use the delete Operator
Copy codeThe Code is as follows:
Delete person. american; // you can delete the attributes of an object by yourself.
Delete cannot delete objects.

4. hasOwnProperty () method and isPrototypeOf () method
In fact, these two methods may be the same as I learned here, but it doesn't matter. You can skip it, when we learn inheritance, you can look back,
You will understand.
4.1: hasOwnProperty () method. If the object uses the name specified by a single string parameter to locally define a non-inherited property, true is returned. Otherwise, false is returned.
Copy codeThe Code is as follows:
Function House (price, area, developers)
{
This. price = price;
This. area = area;
This. developers = developers;
}
House. prototype. housevalue = function () {return this. price * this. area ;}
Function HouseSon (price, area, developers, city)
{
House. call (this, price, area, developers );
This. city = city;
}
HouseSon. prototype = new House (House, 80, "vanke"); // get House attributes
Delete HouseSon. prototype. price; // delete
Delete HouseSon. prototype. area;
Delete HouseSon. prototype. developers;
HouseSon. prototype. container = function () {return "container" + this. price * this. area ;}
For (var I in HouseSon. prototype)
{
Document. write (I + "<br/> ");
}
Var house = new HouseSon (20000,180, "vanke", "shenzhen ");
Document. write (house. container () + "<br/> ");
Document. write (house. housevalue () + "<br/> ");
Document. write (house. hasOwnProperty ("housevalue") + "<br/>"); // This is the prototype
Document. write (house. hasOwnProperty ("price") + "<br/>"); // local

Javascript: Array
An array is an ordered set. Each element in an array has a digital position and can be accessed with a small logo. Because javascript is a non-data type language, therefore, different types can be included.
1. Create an array
Copy codeThe Code is as follows:
Var array = [] // array containing no elements
Var person = ["Frank", 22, 'male']; // array with different elements
Var value = 100;
Var num = [value + 12, value-23, value * 2]; // supported expressions
// Of course, Array can also be used to create parameters of different types, such as objects and arrays.

2. add, delete, and traverse arrays.
Since adding and traversing are relatively simple, we will not illustrate it, but let's talk about deleting it!
Copy codeThe Code is as follows:
Function diaplayarray (arr) // function used to execute a print task
{
If (! Arr) return;
For (var num = 0; num <arr. length; num ++)
{
Document. write ("Num is" + arr [num] + "\ t ");
}
Document. write ("" + "<br/> ");
}
Var array = [2, 32, 14, 57, 6];
Document. write (array. shift () + "<br/>"); // Delete the first entry in the array. The deleted Value 2 is returned.
Document. write (array. pop () + "<br/>"); // Delete the last one in the array. The deleted Value 6 is returned.
Document. write (array. join ("*") + "<br/>"); // returns a string 32*14*57 by concatenating array elements *.
Document. write (array. push (100) + "<br/>"); // Add an array element
Array. reverse (); // reverse the order of array elements
Diaplayarray (array); // output Num is 100 Num is 5 Num is 4 Num is 3
Array. splice (300,600, 300,600); // Delete (including the second) from the second array to the third, and the last is the newly inserted value.
Diaplayarray (array); // output Num is 100 Num is 300 Num is 600 Num is 32

Conclusion: comrades have worked hard ..........
Here is an introduction to objects and arrays. Next we will go to the javascript client.

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.