JavaScript 继承 父类型 子类型 原型链继承 构造函数

文章目录

代码图

在这里插入图片描述

代码

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>03_组合继承</title>
</head>
<body>
<!--
方式3: 原型链+借用构造函数的组合继承
1. 利用原型链实现对父类型对象的方法继承
2. 利用super()借用父类型构建函数初始化相同属性
-->
<script type="text/javascript">
// 父类型-----------------------------------------
function Person(name,age){
  this.name = name
  this.age = age
}
Person.prototype.sayName = function(){
  console.log(this.name);
}
// 子类型-------------------------------------------
function Student(name,age,num){
  Person.call(this,name,age)//为了使用父类型的属性
  this.num = num
}
// 为了使用父类的方法
Student.prototype = new Person()
// 在父类型的实例中添加构造器和自己的方法
Student.prototype.constructor = Student
Student.prototype.sayNum = function(){
  console.log(this.num);
}
// 创建实例--------------------------------------------
var per = new Student('Alice',20,'20202118')
per.sayName()
per.sayNum()

</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值