新聞中心
我們知道生活中客觀存在的一切事物皆為對(duì)象,那在程序中的對(duì)象是什么樣子呢?我們可以將程序中的對(duì)象理解為客戶端世界中的對(duì)象在一種計(jì)算機(jī)中的一種表示方式.所有的編程語言中提到的對(duì)象其性質(zhì)都是類似的,它往往對(duì)應(yīng)內(nèi)存中的一塊區(qū)域,在這個(gè)區(qū)域中存儲(chǔ)對(duì)象的屬性或方法信息。

成都創(chuàng)新互聯(lián)是一家專注于成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、成都外貿(mào)網(wǎng)站建設(shè)與策劃設(shè)計(jì),仁和網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:仁和等地區(qū)。仁和做網(wǎng)站價(jià)格咨詢:18980820575
一、類
在面向?qū)ο缶幊讨校瑢?duì)象是一個(gè)類的實(shí)例,類定義了一組公開的屬性和方法。類簡化了同一類型的多個(gè)對(duì)象的創(chuàng)建。
1 var star = {}; //組裝一個(gè)star對(duì)象 2 star[“Polaris”] = new Object; 3 star[“Mizar”] = new Object; 4 star[“Polaris”].constellation = “Ursa Minor”; 5 star[“Mizar”].constellation = “Ursa Major”;
以下為使用偽類組裝一個(gè)star對(duì)象:【主要目的是:簡化代碼重復(fù)率,提高閱讀效率】
1 var star = {};
2 function Star(constell,type,specclass,magnitude) {
3 this.constellation = constell;
4 this.type = type;
5 this.spectralClass = specclass;
6 this.mag = magnitude;
7 }
8
9 star["Polaris"] = new Star("Ursa Minor","Double/Cepheid","F7",2.0);
10 star["Mizar"] = new Star("Ursa Major","Spectroscopic Binary","A1 V",2.3);
二、創(chuàng)建對(duì)象
2.1 兩種方法創(chuàng)建對(duì)象:
1 var star=new Object; //new關(guān)鍵字
2 var star={} //花括號(hào)
2.2 為對(duì)象添加屬性
1 star.name="Polaris";
2 star.constellation="Ursa Minor";
2.3 遍歷對(duì)象屬性 用for…in..函數(shù)
1 function Star(constell,type,specclass,magnitude) {
2 this.constellation = constell;
3 this.type = type;
4 this.spectralClass = specclass;
5 this.mag = magnitude;
6 }
7 star["Polaris"] = new Star("Ursa Minor","Double/Cepheid","F7",2.0);
8 star["Mizar"] = new Star("Ursa Major","Spectroscopic Binary","A1 V",2.3);
9 star["Aldebaran"] = new Star("Taurus","Irregular Variable","K5 III",0.85);
10 star["Rigel"] = new Star("Orion","Supergiant with Companion","B8 Ia",0.12);
11
12 for (var element in star) { //元素名
13 for (var propt in star[element]) { //屬性值
14 alert(element + ": " + propt + " = " + star[element][propt]);
15 }
16 }
名稱欄目:講解一下JavaScript中的對(duì)象
轉(zhuǎn)載來源:http://www.dlmjj.cn/article/ccishcj.html


咨詢
建站咨詢
