1. 程式人生 > >JavaScript封裝一個物件

JavaScript封裝一個物件

function Car(sColor,iDoors,iMpg) {
  this.color = sColor;
  this.doors = iDoors;
  this.mpg = iMpg;
  this.drivers = new Array("Mike","John");
  this.test=function(){
    console.log("test");
  }
}

Car.prototype.showColor = function() {
  alert(this.color);
};
var objCar = new Car("red", 1, 2);