1 function CreatePerson(name,sex){ 2 this.name=name; 3 this.sex=sex; 4 this.showName(); 5 this.showSex(); 6 } 7 CreatePerson.prototype={ 8 constructor:CreatePerson,//修正constructor 9 showName:function(){ 10 alert(this.name); 11 }, 12 showSex:function(){ 13 alert(this.sex); 14 } 15 } 16 new CreatePerson(‘小明‘,‘男‘); 17 18 function CreateStar(name,sex,job){ 19 CreatePerson.call(this,name,sex);//继承CreatePerson属性 20 this.job=job; 21 this.showJob(); 22 } 23 24 copyObj(CreateStar.prototype,CreatePerson.prototype);//拷贝继承CreatePerson.prototype的所有方法 25 26 CreateStar.prototype.showJob=function(){ 27 alert(this.job); 28 29 } 30 new CreateStar(‘黄晓明‘,‘男‘,‘演员‘); 31 32 //拷贝父类的所有方法到子类 33 function copyObj(newObj,oldObj){ 34 for(attr in oldObj){ 35 newObj[attr]=oldObj[attr]; 36 } 37 }
时间: 2024-10-06 17:04:33