电脑技术学习

javascript混合的构造函数/原型方式

dn001

即用构造函数定义对象的所有飞函数属性,用原型方式定义对象的函数属性(方法)。


Code
<script type="text/javascript">
debugger;
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);


};
var Car1 = new Car("red", 4, 23);
var Car2 = new Car("blue", 3, 25);

Car1.drivers.push("Matt");

alert(Car1.drivers);
alert(Car2.drivers);

</script>