构造函数+原型

<script type="text/javascript">
function Car(color, doors, wheels) { //创建一个类
this.color = color; //类属性
this.doors = doors;
this.wheels = wheels;
this.run = function() { //类方法
alert("我会跑!!!");
}
}
Car.prototype.cc = "hahaha"; //通过proto属性存储一个cc=“hahaha”;
Car.prototype.stop = function() { //原型链
alert("我会停止!!!");
}
Car.prototype.stop2 = function() { //关注原型链接
alert("我还是会停!!!");
}

//创建一个实例对象;
var car1 = new Car("red", 4, 4);
alert(car1.cc);
car1.run();
car1.stop();
document.write("这个车的颜色是:" + car1.color + "<br/>");
document.write("这个车有" + car1.doors + "个门" + "<br/>");
document.write("这个车有" + car1.wheels + "轮子<br/><br/><br/>");


var car2 = new Car("blue", 4, 4);
document.write("这个车的颜色是:" + car2.color + ",有" + car2.doors + "个车门,有" + car2.wheels + "个轮子<br/>");
car2.stop2();
</script>
原文地址:https://www.cnblogs.com/ermaoblog/p/7458303.html