Javascript中定义类或对象

 推荐一个我最常用的方式(来自http://www.w3school.com.cn/js/pro_js_object_defining.asp)。

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

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

var oCar1 = new Car("red",4,23);
var oCar2 = new Car("blue",3,25);

oCar1.drivers.push("Bill");

alert(oCar1.drivers);    //输出 "Mike,John,Bill"
alert(oCar2.drivers);    //输出 "Mike,John"

 

posted @ 2013-08-19 10:52  bupt_ding  阅读(111)  评论(0)    收藏  举报