国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

javascript 類定義的4種方法

2024-05-06 14:14:02
字體:
來源:轉載
供稿:網友
代碼如下:
/*
工廠方式--- 創建并返回特定類型的對象的 工廠函數 ( 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;
}

/*
構造函數方式--- 構造函數看起來很像工廠函數
*/
function Car(color,doors,mpg){
this.color = color;
this.doors = doors;
this.mpg = mpg;
this.showCar = function(){
alert(this.color);
};
}
/*
原型方式--- 利用了對象的 prototype 屬性,可把它看成創建新對象所依賴的原型
*/
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);
};

/*
混合的構造函數 /原型方式--- 用構造函數定義對象的所有非函數屬性,用原型方式定義對象的函數屬性(方法)
*/
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);
};
/*
動態原型方法--- 在構造函數內定義非函數屬性,而函數屬性則利用原型屬性定義。唯一的區別是賦予對象方法的位置。
*/
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;
}
} //該方法使用標志( _initialized )來判斷是否已給原型賦予了任何方法。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 黄梅县| 丁青县| 镇宁| 乌拉特中旗| 巫溪县| 九江县| 厦门市| 双牌县| 安西县| 通城县| 湖南省| 防城港市| 稷山县| 武清区| 玛曲县| 三原县| 永顺县| 班玛县| 南溪县| 皋兰县| 大港区| 茂名市| 宁阳县| 龙川县| 恩平市| 当涂县| 罗江县| 游戏| 桐柏县| 宜丰县| 高唐县| 肇源县| 桐柏县| 绵竹市| 东乌珠穆沁旗| 渭源县| 邯郸市| 密山市| 泰安市| 阿合奇县| 苏州市|