js——继承
继承:两个类之间的 is a kind of 关系
实现继承:子类拥有父类的全部属性和方法,还可以拥有自己独有的方法和属性
通过原型链实现继承:子类的原型指向父类的实例 子类.prototype=new 父类() 子类可以重写/复写父类的方法
在这里插入代码片
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>js继承:子类的原型指向父类的实例</title>
</head>
<body>
<script>
//
function People(name, age) {
this.name = name
this.age = age
}
People.prototype.sayHello = function () {
console.log(this.name + ":hello");
}
// 这里,子类的参数要包含父类的参数
function Student(name, age, sex) {
this.name = name
this.age = age
this.sex = sex
}
// 创建出子类后书写继承语句 子类.prototype=new 父类()
Student.prototype = new People()
Student.prototype.jude = function () {
console.log(this.sex);
}
// 测试
var stu = new Student('sam', 12, 'woman')
stu.sayHello()
stu.jude()
</script>
</body>
</html>