信息发布→ 登录 注册 退出

javascript中最常用的继承模式 组合继承

发布时间:2026-01-11

点击量:
复制代码 代码如下:
<script type="text/javascript">
//创建基类
function Person(name, age) {
this.name = name;
this.age = age;
}
//通过原型方式给基类添加函数(这样可以服用此函数)
Person.prototype.showName = function () {
alert(this.name);
}
//创建子类
function Student(name, age, score) {
this.score = score;
Person.call(this,name,age);
}
//把父类的实例赋值给子类的原型
Student.prototype = new Person();
//通过原型方式给子类添加函数(这样可以服用此函数)
Student.prototype.showScore = function () {
alert(this.score);
}

//以下为使用
var student = new Student("zhangsan", 22, 100);
student.showName();
student.showScore();

var stu = new Student("lisi", 25, 200);
stu.showName();
stu.showScore();
</script>
在线客服
服务热线

服务热线

4008888355

微信咨询
二维码
返回顶部
×二维码

截屏,微信识别二维码

打开微信

微信号已复制,请打开微信添加咨询详情!