javascript中類的定義方式詳解(四種方式)
本文實(shí)例講述了javascript中類的定義方式。分享給大家供大家參考,具體如下:
類的定義包括四種方式:
1、工廠方式
function createCar(name,color,price){
var tempcar=new Object;
tempcar.name=name;
tempcar.color=color;
tempcar.price=price;
tempcar.getName=function(){
document.write(this.name+"-----"+this.color+"<br>");
};
return tempcar;
}
var car1=new createCar("工廠桑塔納","red","121313");
car1.getName();
定義了一個(gè)能創(chuàng)建并返回特定類型對(duì)象的工廠函數(shù), 看起來(lái)還是不錯(cuò)的, 但有個(gè)小問(wèn)題 ,
每次調(diào)用時(shí)都要?jiǎng)?chuàng)建新函數(shù) showColor,我們可以把它移到函數(shù)外面,
function getName(){
document.write(this.name+"-----"+this.color+"<br>");
}
在工廠函數(shù)中直接指向它
這樣避免了重復(fù)創(chuàng)建函數(shù)的問(wèn)題,但看起來(lái)不像對(duì)象的方法了。
2、構(gòu)造函數(shù)方式
function Car(name,color,price){
this.name=name;
this.color=color;
this.price=price;
this.getColor=function(){
document.write(this.name+"-----"+this.color+"<br>");
};
}
var car2=new Car("構(gòu)造桑塔納","red","121313");
car2.getColor();
可以看到與第一中方式的差別,在構(gòu)造函數(shù)內(nèi)部無(wú)創(chuàng)建對(duì)象,而是使用 this 關(guān)鍵字。
使用 new 調(diào)用構(gòu)造函數(shù)時(shí),先創(chuàng)建了一個(gè)對(duì)象,然后用 this 來(lái)訪問(wèn)。
這種用法于其他面向?qū)ο笳Z(yǔ)言很相似了, 但這種方式和上一種有同一個(gè)問(wèn)題, 就是重復(fù)創(chuàng)建函數(shù)。
3、原型方式
function proCar(){
}
proCar.prototype.name="原型";
proCar.prototype.color="blue";
proCar.prototype.price="10000";
proCar.prototype.getName=function(){
document.write(this.name+"-----"+this.color+"<br>");
};
var car3=new proCar();
car3.getName();
首先定義了構(gòu)造函數(shù) Car,但無(wú)任何代碼,然后通過(guò) prototype 添加屬性。優(yōu)點(diǎn):
a. 所有實(shí)例存放的都是指向 showColor 的指針,解決了重復(fù)創(chuàng)建函數(shù)的問(wèn)題
b. 可以用 instanceof 檢查對(duì)象類型
缺點(diǎn),添加下面的代碼:
proCar.prototype.drivers = newArray("mike", "sue");
car3.drivers.push("matt");
alert(car3.drivers);//outputs "mike,sue,matt"
alert(car3.drivers);//outputs "mike,sue,matt"
drivers 是指向 Array 對(duì)象的指針,proCar 的兩個(gè)實(shí)例都指向同一個(gè)數(shù)組。
4、動(dòng)態(tài)原型方式
function autoProCar(name,color,price){
this.name=name;
this.color=color;
this.price=price;
this.drives=new Array("mike","sue");
if(typeof autoProCar.initialized== "undefined"){
autoProCar.prototype.getName =function(){
document.write(this.name+"-----"+this.color+"<br>");
};
autoProCar.initialized=true;
}
}
var car4=new autoProCar("動(dòng)態(tài)原型","yellow","1234565");
car4.getName();
car4.drives.push("newOne");
document.write(car4.drives);
這種方式是我最喜歡的, 所有的類定義都在一個(gè)函數(shù)中完成, 看起來(lái)非常像其他語(yǔ)言的類定義,不會(huì)重復(fù)創(chuàng)建函數(shù),還可以用 instanceof
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
相關(guān)文章
微信小程序調(diào)用天氣接口并且渲染在頁(yè)面過(guò)程詳解
這篇文章主要介紹了微信小程序調(diào)用天氣接口并且渲染在頁(yè)面過(guò)程詳解,今天寫一個(gè)具體的例子,調(diào)用一個(gè)免費(fèi)的天氣接口的api,并且把所獲取的內(nèi)容展示在前端的界面,前端界面與 iView Weapp結(jié)合,需要的朋友可以參考下2019-06-06
js學(xué)習(xí)筆記之class類、super和extends關(guān)鍵詞
es6提供了一個(gè)新語(yǔ)法就是class,下面這篇文章主要給大家介紹了關(guān)于js學(xué)習(xí)筆記之class類、super和extends關(guān)鍵詞的相關(guān)資料,需要的朋友可以參考下2021-08-08
element 中 el-menu 組件的無(wú)限極循環(huán)思路代碼詳解
這篇文章主要介紹了element 中 el-menu 組件的無(wú)限極循環(huán),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
javascript中幾個(gè)容易混淆的概念總結(jié)
這篇文章主要介紹了javascript中幾個(gè)容易混淆的概念總結(jié),都是平時(shí)經(jīng)常遇到的問(wèn)題,這里推薦給大家,有需要的小伙伴參考下吧。2015-04-04
JS實(shí)現(xiàn)動(dòng)態(tài)增加和刪除li標(biāo)簽行的實(shí)例代碼
下面小編就為大家?guī)?lái)一篇JS實(shí)現(xiàn)動(dòng)態(tài)增加和刪除li標(biāo)簽行的實(shí)例代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10

