今天我们来研究下javascript中如何用构造函数创建对象以及子类的继承
第一步 创建一个 基类对象 car;
function Car(name,id,tyep){ this.name=name;//车辆品牌 this.id=id;//车牌号 this.type=type;//车型 } //使用prototype区域创建方法,使用该方法创建对象方法的优点是 相当于 c++语言中的 public类型方法; Car.prototype.run=function(){ console.log(this.name+"run");//这辆车run! } function Bigcar(name,id,type,chelun){ //JS中子类继承基类的方法是 使用 基类.call() 方法 Car.call(this,name,id,type); this.chelun=chelun; } //JS中 对象的prototype属性只能用 for in 方法遍历 for(var i in Car.prototype){ Bigcar.prototype[i]=Car.prototype[i]; } Bigcar.prototype.function didi(){ console.log(this.chelun+"didi"); }