1.工厂方式
//定义
var oCar = new Object();
oCar.color = “red”;
oCar.doors = 4;
oCar.showColor = function() {
alert(this.color);
}
2.造函数
//定义
function Car(color, doors) {
this.color = color;
this.doors = doors;
this.showColor = function() {
alert(this.color);
};
3.混合的构造函数/原型模式
function Car(color,doors)
{
this.color=color;
this.doors=doors;
this.drivers=new Array(“Tom”,”Jerry”);
}
Car.prototype.showColor=function(){
alert(this.color);
}
javascript常用创建对象的方式
最新推荐文章于 2024-11-04 10:13:26 发布