<!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>继承</title>
</head>
<body>
<script>
function Person(name, age) {
this.name = name
this.age = age
}
Person.prototype.speak = function () {
console.log(this.name + 'aaaa啊啊啊啊')
}
Person.prototype.eat = function () {
console.log(this.name + "吃吃吃")
}
//原型链继承
function Student(name,age) {
}
Student.prototype = new Person("小杨", "18")
var s1 = new Student("xiaoyang", "21")//不执行
s1.eat()//可以执行
console.log(s1);//得到的是 小杨 18
//冒充继承
function Teacher(name,age){
Person.call(this,name,age)
}
var t=new Teacher("小杨","18")
console.log(t)
// t.eat() 不执行
//混合继承
function Student(name,age){
Person.call(this,name,age)
}
Student.prototype=new Person()
var s2=new Student("小小杨","22")
console.log(s2)
s2.eat()//执行
</script>
</body>
</html>
关于js继承的演示
猜你喜欢
转载自blog.csdn.net/m0_60324420/article/details/121735001
今日推荐
周排行