JavaScript 基礎(chǔ)篇之對(duì)象、數(shù)組使用介紹(三)
更新時(shí)間:2012年04月07日 02:05:46 作者:
對(duì)象我們?cè)谇懊嬉埠?jiǎn)單介紹過(guò),它是一種將多個(gè)數(shù)據(jù)值集中在一個(gè)單元的東西,使用名字來(lái)存取,它是一個(gè)無(wú)序的屬性集合
Javascript:對(duì)象
對(duì)象我們?cè)谇懊嬉埠?jiǎn)單介紹過(guò),它是一種將多個(gè)數(shù)據(jù)值集中在一個(gè)單元的東西,使用名字來(lái)存取,它是一個(gè)無(wú)序的屬性集合。
1、創(chuàng)建對(duì)象的幾種方式
var empty = {} //創(chuàng)建一個(gè)沒(méi)有屬性的對(duì)象。
var person = {name:"ben",age:22,sex:'男'}//使用直接量創(chuàng)建對(duì)象
var people = {{name:'Frank',age:21},{name:'Mary',age:21},sex:'MAN'}// 對(duì)象的元素可以是對(duì)象
2、對(duì)象屬性
var person = {}; //創(chuàng)建一個(gè)對(duì)象
person.name = "Frank"; //添加屬性
person.country = "china";
person.age = 22;
person.american = new Object(); //這個(gè)屬性是個(gè)對(duì)象
person.american.name = "Lisa";
person.american.country = "American";
person.american.age = 20;
function displayperson(personmore) //打印上面的對(duì)象
{
for(var p in personmore) //枚舉循環(huán)
{
if(typeof(personmore[p]) == "object")//判斷類(lèi)型
{
for(var o in personmore[p])
{
document.write("American people :" +o+"\t" +personmore[p][o]+"<br />");
}
document.write("<br />");
continue;//結(jié)束本次循環(huán),進(jìn)行下一次循環(huán).
document.write("china people :"+ p+"\t" + personmore[p] +"<br />");
}
}
displayperson(person);//調(diào)用函數(shù)
//輸出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運(yùn)算符
delete person.american;//就可以自己刪除對(duì)象的屬性
delete 是不能刪除對(duì)象的。
4、hasOwnProperty()方法和isPrototypeOf()方法
其實(shí)這兩個(gè)方法,在這里說(shuō)初學(xué)的朋友可能會(huì)和我當(dāng)時(shí)學(xué)到這里一樣,看不懂,但是沒(méi)關(guān)系,可以跳過(guò)去,到時(shí)候我們學(xué)到繼承的時(shí)候你在回頭看看,
就明白了。
4.1:hasOwnProperty()方法,如果對(duì)象用一個(gè)單獨(dú)字符串參數(shù)所指定的名字來(lái)本地定義一個(gè)非繼承的屬性,就返回true。否則返回false。
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");//獲取House的屬性
delete HouseSon.prototype.price;//刪除
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 />");//這是原型
document.write(house.hasOwnProperty("price")+"<br />");//本地
Javascript:數(shù)組
數(shù)組是一個(gè)有序的集合,每個(gè)元素在數(shù)組中都有一個(gè)數(shù)字化的位置,可以使用小標(biāo)訪問(wèn),由于javascript是一種非數(shù)據(jù)類(lèi)型的語(yǔ)言,所以里面可以包涵不同類(lèi)型。
1、數(shù)組的創(chuàng)建
var array = [] //不包涵任何元素的數(shù)組
var person = ["Frank",22,'男 '];//帶不同元素的數(shù)組
var value = 100;
var num = [value+12,value-23,value*2];//支持表達(dá)式
//當(dāng)然還有使用Array來(lái)創(chuàng)建,可以有不同類(lèi)型的參數(shù),可以是對(duì)象,數(shù)組等。
2、數(shù)組的添加,刪除,遍歷。
由于添加,遍歷都比較簡(jiǎn)單,就不舉例說(shuō)明,但是這么還是說(shuō)說(shuō)刪除吧!
function diaplayarray(arr) //執(zhí)行打印任務(wù)的函數(shù)
{
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 />"); //刪除數(shù)組中的第一個(gè),返回刪除的值2
document.write(array.pop()+"<br />"); //刪除數(shù)組中的最后一個(gè),返回刪除的值6
document.write(array.join("*")+"<br />");//將數(shù)組元素用*連接返回一個(gè)字符串32*14*57
document.write(array.push(100) +"<br />");//添加數(shù)組元素
array.reverse();//顛倒數(shù)組元素順序
diaplayarray(array);//輸出Num is 100 Num is 5 Num is 4 Num is 3
array.splice(1,2,300,600);//從數(shù)組第二個(gè)開(kāi)始刪除(含第二個(gè)),到第三個(gè),后面300,600是新插入的值
diaplayarray(array);//輸出Num is 100 Num is 300 Num is 600 Num is 32
小結(jié):同志們辛苦了..........
關(guān)于對(duì)象和數(shù)組就介紹到這里吧,接下來(lái)我們馬上就要到j(luò)avascript客戶端了。
對(duì)象我們?cè)谇懊嬉埠?jiǎn)單介紹過(guò),它是一種將多個(gè)數(shù)據(jù)值集中在一個(gè)單元的東西,使用名字來(lái)存取,它是一個(gè)無(wú)序的屬性集合。
1、創(chuàng)建對(duì)象的幾種方式
復(fù)制代碼 代碼如下:
var empty = {} //創(chuàng)建一個(gè)沒(méi)有屬性的對(duì)象。
var person = {name:"ben",age:22,sex:'男'}//使用直接量創(chuàng)建對(duì)象
var people = {{name:'Frank',age:21},{name:'Mary',age:21},sex:'MAN'}// 對(duì)象的元素可以是對(duì)象
2、對(duì)象屬性
復(fù)制代碼 代碼如下:
var person = {}; //創(chuàng)建一個(gè)對(duì)象
person.name = "Frank"; //添加屬性
person.country = "china";
person.age = 22;
person.american = new Object(); //這個(gè)屬性是個(gè)對(duì)象
person.american.name = "Lisa";
person.american.country = "American";
person.american.age = 20;
function displayperson(personmore) //打印上面的對(duì)象
{
for(var p in personmore) //枚舉循環(huán)
{
if(typeof(personmore[p]) == "object")//判斷類(lèi)型
{
for(var o in personmore[p])
{
document.write("American people :" +o+"\t" +personmore[p][o]+"<br />");
}
document.write("<br />");
continue;//結(jié)束本次循環(huán),進(jìn)行下一次循環(huán).
document.write("china people :"+ p+"\t" + personmore[p] +"<br />");
}
}
displayperson(person);//調(diào)用函數(shù)
//輸出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運(yùn)算符
復(fù)制代碼 代碼如下:
delete person.american;//就可以自己刪除對(duì)象的屬性
delete 是不能刪除對(duì)象的。
4、hasOwnProperty()方法和isPrototypeOf()方法
其實(shí)這兩個(gè)方法,在這里說(shuō)初學(xué)的朋友可能會(huì)和我當(dāng)時(shí)學(xué)到這里一樣,看不懂,但是沒(méi)關(guān)系,可以跳過(guò)去,到時(shí)候我們學(xué)到繼承的時(shí)候你在回頭看看,
就明白了。
4.1:hasOwnProperty()方法,如果對(duì)象用一個(gè)單獨(dú)字符串參數(shù)所指定的名字來(lái)本地定義一個(gè)非繼承的屬性,就返回true。否則返回false。
復(fù)制代碼 代碼如下:
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");//獲取House的屬性
delete HouseSon.prototype.price;//刪除
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 />");//這是原型
document.write(house.hasOwnProperty("price")+"<br />");//本地
Javascript:數(shù)組
數(shù)組是一個(gè)有序的集合,每個(gè)元素在數(shù)組中都有一個(gè)數(shù)字化的位置,可以使用小標(biāo)訪問(wèn),由于javascript是一種非數(shù)據(jù)類(lèi)型的語(yǔ)言,所以里面可以包涵不同類(lèi)型。
1、數(shù)組的創(chuàng)建
復(fù)制代碼 代碼如下:
var array = [] //不包涵任何元素的數(shù)組
var person = ["Frank",22,'男 '];//帶不同元素的數(shù)組
var value = 100;
var num = [value+12,value-23,value*2];//支持表達(dá)式
//當(dāng)然還有使用Array來(lái)創(chuàng)建,可以有不同類(lèi)型的參數(shù),可以是對(duì)象,數(shù)組等。
2、數(shù)組的添加,刪除,遍歷。
由于添加,遍歷都比較簡(jiǎn)單,就不舉例說(shuō)明,但是這么還是說(shuō)說(shuō)刪除吧!
復(fù)制代碼 代碼如下:
function diaplayarray(arr) //執(zhí)行打印任務(wù)的函數(shù)
{
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 />"); //刪除數(shù)組中的第一個(gè),返回刪除的值2
document.write(array.pop()+"<br />"); //刪除數(shù)組中的最后一個(gè),返回刪除的值6
document.write(array.join("*")+"<br />");//將數(shù)組元素用*連接返回一個(gè)字符串32*14*57
document.write(array.push(100) +"<br />");//添加數(shù)組元素
array.reverse();//顛倒數(shù)組元素順序
diaplayarray(array);//輸出Num is 100 Num is 5 Num is 4 Num is 3
array.splice(1,2,300,600);//從數(shù)組第二個(gè)開(kāi)始刪除(含第二個(gè)),到第三個(gè),后面300,600是新插入的值
diaplayarray(array);//輸出Num is 100 Num is 300 Num is 600 Num is 32
小結(jié):同志們辛苦了..........
關(guān)于對(duì)象和數(shù)組就介紹到這里吧,接下來(lái)我們馬上就要到j(luò)avascript客戶端了。
相關(guān)文章
javascript中call apply 與 bind方法詳解
網(wǎng)上文章雖多,大多復(fù)制粘貼,且晦澀難懂,我希望能夠通過(guò)這篇文章,能夠清晰的提升對(duì)apply、call、bind的認(rèn)識(shí),并通過(guò)一些具體的示例給大家展示下這3個(gè)方法的用法,希望大家能夠喜歡。2016-03-03
javascript學(xué)習(xí)筆記(十七) 檢測(cè)瀏覽器插件代碼
javascript學(xué)習(xí)筆記之檢測(cè)瀏覽器插件代碼,需要的朋友可以參考下2012-06-06
JavaScript基礎(chǔ)知識(shí)點(diǎn)歸納(推薦)
下面小編就為大家?guī)?lái)一篇JavaScript基礎(chǔ)知識(shí)點(diǎn)歸納(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07
JS中for循序中延遲加載動(dòng)態(tài)效果的具體實(shí)現(xiàn)
今天在做一個(gè)前端的效果的時(shí)候碰到一個(gè)棘手的問(wèn)題,就是實(shí)現(xiàn)一個(gè)動(dòng)態(tài)的圓柱效果,廢話不多少,直接上代碼2013-08-08
JavaScript中splice與slice的區(qū)別
本文給大家分享的是JavaScript中的splice和slice的用法和區(qū)別,slice()方法和splice()方法都是原生js中對(duì)數(shù)組操作的方法,下面我們來(lái)詳細(xì)探討下2017-05-05

