Home >Web Front-end >JS Tutorial >Introduction to the use of objects and arrays in the basics of JavaScript (3)_Basic knowledge

Introduction to the use of objects and arrays in the basics of JavaScript (3)_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:54:36935browse
Javascript: Object
We have briefly introduced the object before. It is a thing that concentrates multiple data values ​​​​in one unit. It is accessed by name. It is an unordered attribute. gather.
1. Several ways to create objects
Copy code The code is as follows:

var empty = {} //Create an object with no properties.
var person = {name:"ben",age:22,sex:'male'}//Use direct quantities to create objects
var people = {{name:'Frank',age:21},{ name:'Mary',age:21},sex:'MAN'}// The elements of the object can be objects

2. Object attributes
Copy code The code is as follows:

var person = {}; //Create an object
person.name = "Frank"; // Add attribute
person.country = "china";
person.age = 22;
person.american = new Object(); //This attribute 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")//Judge the type
{
for(var o in personmore[ p])
{
document.write("American people :" o "t" personmore[p][o] "
");
}
document.write ("
");
continue;//End this cycle and proceed to the next cycle.
document.write("china people :" p "t" personmore[p] "< ;br />");
}
}
displayperson(person);//Call 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. To delete an attribute
use the delete operator
Copy the code The code is as follows:

delete person. american;//You can delete the object's attributes yourself
delete cannot delete the object.

4. hasOwnProperty() method and isPrototypeOf() method
In fact, these two methods, beginner friends may be the same as I learned here, they can’t understand them, but It doesn't matter, you can skip it. When we learn about inheritance, you will understand when you look back
.
4.1: The hasOwnProperty() method returns true if the object locally defines a non-inherited property with the name specified by a separate string parameter. Otherwise return false.
Copy code The 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(10000,80,"vanke");//Get the properties of House
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 "
");
}
var house = new HouseSon(20000,180,"vanke","shenzhen");
document .write(house.container() "
");
document.write(house.housevalue() "
");
document.write(house.hasOwnProperty ("housevalue") "
");//This is the prototype
document.write(house.hasOwnProperty("price") "
");//Local

Javascript: Array
An array is an ordered collection. Each element has a numeric position in the array and can be accessed using subscripts. Since javascript is a A non-data type language, so it can contain different types.
1. Creation of array
Copy code The code is as follows:

var array = [] //Array without any elements
var person = ["Frank",22,'male'];//Array with different elements
var value = 100;
var num = [value 12, value-23, value*2];//Supports expressions
//Of course, it can also be created using Array, which can have different types of parameters, which can be objects. Array etc.

2. Add, delete, and traverse arrays.
Since addition and traversal are relatively simple, I won’t give examples, but let’s talk about deletion!
Copy code The code is as follows:

function diaplayarray(arr) //Function to perform printing tasks
{
if(!arr)return;
for(var num =0;num{
document.write("Num is " arr[num ] "t");
}
document.write(" " "
");
}
var array = [2,32,14,57,6] ;
document.write(array.shift() "
"); //Delete the first one in the array and return the deleted value 2
document.write(array.pop() "
"); //Delete the last one in the array and return the deleted value 6
document.write(array.join("*") "
");/ /Concatenate the array elements with * to return a string 32*14*57
document.write(array.push(100) "
");//Add array elements
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(1,2,300,600);//From the second array Start deleting the first one (including the second one), and then go to the third one, followed by 300 and 600, which are the newly inserted values ​​
diaplayarray(array);//Output Num is 100 Num is 300 Num is 600 Num is 32

Summary: Comrades, thank you for your hard work...
That’s all about objects and arrays. Next, we will get to the javascript client soon.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn