Javascript: Objects
Object we introduced earlier, it is a set of multiple data values in a unit of things, using the name to access, it is an unordered collection of attributes.
1, create the object of several ways
Copy Code code as follows:
var empty = {}//Create an object with no attributes.
var person = {Name: "Ben", Age:22,sex: ' Male '}//create object using direct amount
var people = {{name: ' Frank ', age:21},{name: ' Mary ', age:21},sex: ' Man '}//the element of the object can be an object
2, Object properties
Copy Code code as follows:
var person = {}; Create an Object
Person.name = "Frank"; Add properties
Person.country = "the";
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 loops
{
if (typeof (Personmore[p]) = = "Object")//Judge type
{
for (Var o in personmore[p])
{
document.write ("American People:" +o+ "T" +personmore[p][o]+ "<br/>");
}
document.write ("<br/>");
continue;//end this cycle and make the next loop.
document.write ("People:" + p+ "T" + personmore[p] + "<br/>");
}
}
Displayperson (person);//Call function
Export People:name Frank
People:country
People:age 22
American People:name Lisa
American People:country American
American People:age 20
3. Delete Attributes
Using the delete operator
Copy Code code as follows:
Delete person.american;//You can remove the properties of the object yourself
Delete cannot delete an object.
4, hasOwnProperty () method and isPrototypeOf () method
In fact, these two methods, here that the beginner's friends May and I learned here, do not understand, but it does not matter, you can jump over, then we learn to inherit when you look back,
Will understand.
The 4.1:hasownproperty () method returns True if the object defines a property that is not inherited locally with the name specified by a single string parameter. otherwise returns false.
Copy Code code 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 (10000,80, "Vanke");//Get House Properties
Delete houseson.prototype.price;//deletion
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: Arrays
An array is an ordered set of elements, each of which has a digitized position in the array, and can be accessed using small labels, because JavaScript is a non data type language, so it can contain different types.
1, the creation of the array
Copy Code code as follows:
var array = []//no array with any elements
var person = ["Frank", 22, ' Male '];//array with different elements
var value = 100;
var num = [value+12,value-23,value*2];//support expression
Of course, you can use array to create, there are different types of parameters, can be objects, arrays, and so on.
2, the addition of the array, delete, traversal.
Because of the addition, traversal are relatively simple, do not give examples, but this is still said to delete it!
Copy Code code as follows:
function Diaplayarray (arr)//functions to perform print tasks
{
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/>"); Deletes the first in the array and returns the deleted value 2
document.write (Array.pop () + "<br/>"); Deletes the last value in the array, and returns the deleted 6
document.write (Array.join ("*") + "<br/>")//returns a string with the * connection of the array element 32*14*57
document.write (Array.push + "<br/>");//Add array element
Array.reverse ()//reverse array element order
Diaplayarray (array);/output num is num is 5 num is 4 num is 3
Array.splice (1,2,300,600)//From the second start of the array delete (including the second), to the third, followed by 300,600 is the newly inserted value
Diaplayarray (array);/output num is + num is $ num is 32
Summary: Comrades are working hard ....
About objects and arrays here we go, and then we're going to go to the JavaScript client.