本文主要和大家分享js混合继承详解,希望能帮助到大家。
<script type="text/javascript">
window.onload=function(){
//混合继承:原型实现继承+借用构造函数继承
function Person(name,age,gender,wight){
this.name=name;
this.age=age;
this.gender=gender;
this.wight=wight;
}
Person.prototype.sayHi=function(){
console.log("欢迎!");
}
function Student(name,age,gender,wight,score){
Person.call(this,name,age,gender,wight);//实现属性继承
this.score=score;
}
Student.prototype=new Person();//实现方法继承
Student.prototype.sleep=function(){
console.log("请保证充足睡眠!");
}
var stu=new Student("lll",20,"male",150,100);
console.log(stu.name,stu.age,stu.gender,stu.wight,stu.score);
stu.sayHi();
stu.sleep();
var stu2=new Student("222",22,"female",100,110);
console.log(stu2.name,stu2.age,stu2.gender,stu2.wight,stu2.score);
stu2.sayHi();
stu2.sleep();
}
</script>以上就是JS混合继承详解的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号