最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

javascript 類定義的4種方法

 更新時(shí)間:2009年09月12日 21:51:47   作者:  
javascript 類定義的4種方法,大家可以參考下根據(jù)需要選擇。
復(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ù)看起來很像工廠函數(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 )來判斷是否已給原型賦予了任何方法。

相關(guān)文章

最新評(píng)論

宜城市| 布尔津县| 顺昌县| 奉贤区| 夏津县| 拉孜县| 岱山县| 伽师县| 砚山县| 玉田县| 富锦市| 陆川县| 澄迈县| 吴堡县| 灵山县| 东城区| 汶川县| 盐边县| 芜湖市| 收藏| 体育| 牙克石市| 宁陵县| 禹城市| 德州市| 永宁县| 贡嘎县| 河东区| 嫩江县| 阳朔县| 仙游县| 南郑县| 清远市| 丹寨县| 阿荣旗| 泰来县| 绍兴县| 建阳市| 镇巴县| 攀枝花市| 清涧县|