色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

javascript 類定義的4種方法

復(fù)制代碼 代碼如下:
/*
工廠方式--- 創(chuàng)建并返回特定類型的對(duì)象的 工廠函數(shù) ( factory function )
*/
function createCar(color,doors,mpg){
var tempCar = new Object;
tempCar.color = color;
tempCar.doors = doors;
tempCar.mpg = mpg;
tempCar.showCar = function(){
alert(this.color + " " + this.doors);
}
return tempCar;
}

/*
構(gòu)造函數(shù)方式--- 構(gòu)造函數(shù)看起來(lái)很像工廠函數(shù)
*/
function Car(color,doors,mpg){
this.color = color;
this.doors = doors;
this.mpg = mpg;
this.showCar = function(){
alert(this.color);
};
}
/*
原型方式--- 利用了對(duì)象的 prototype 屬性,可把它看成創(chuàng)建新對(duì)象所依賴的原型
*/
function Car(color,doors,mpg){
this.color = color;
this.doors = doors;
this.mpg = mpg;
this.drivers = new Array("nomad","angel");
}

Car.prototype.showCar3 = function(){
alert(this.color);
};

/*
混合的構(gòu)造函數(shù) /原型方式--- 用構(gòu)造函數(shù)定義對(duì)象的所有非函數(shù)屬性,用原型方式定義對(duì)象的函數(shù)屬性(方法)
*/
function Car(sColor, iDoors, iMpg) {
this.color = sColor;
this.doors = iDoors;
this.mpg = iMpg;
this.drivers = new Array("Mike", "Sue");
}

Car.prototype.showColor = function () {
alert(this.color);
};
/*
動(dòng)態(tài)原型方法--- 在構(gòu)造函數(shù)內(nèi)定義非函數(shù)屬性,而函數(shù)屬性則利用原型屬性定義。唯一的區(qū)別是賦予對(duì)象方法的位置。
*/
function Car(sColor, iDoors, iMpg) {
this.color = sColor;
this.doors = iDoors;
this.mpg = iMpg;
this.drivers = new Array("Mike", "Sue");

if (typeof Car._initialized == "undefined") {

Car.prototype.showColor = function () {
alert(this.color);
};

Car._initialized = true;
}
} //該方法使用標(biāo)志( _initialized )來(lái)判斷是否已給原型賦予了任何方法。

JavaScript技術(shù)javascript 類定義的4種方法,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 应城市| 习水县| 蚌埠市| 射洪县| 庐江县| 英超| 马尔康县| 邢台市| 错那县| 望都县| 玉山县| 桓台县| 涪陵区| 宜城市| 邵东县| 清流县| 天台县| 宜君县| 蓬莱市| 甘南县| 教育| 兖州市| 桐城市| 江孜县| 梅河口市| 朝阳区| 双流县| 阿图什市| 舞钢市| 永平县| 兴化市| 阳西县| 桃园县| 克山县| 肃北| 达日县| 盖州市| 贡山| 凉城县| 商河县| 广灵县|