代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>寄生组合继承法</h1>
</body>
<script>
/*
寄生组合继承法
最终继承解决方案(完美方案):寄生组合继承法
* 继承属性:借用构造函数
* 继承方法:原型式继承法
*/
function Person(name,age){
this.name=name;
this.age=age;
}
Person.prototype={
say(){
console.log("说")
},
eit(){
console.log("吃")
},
sleep(){
console.log("睡")
}
}
function Man(name,age){
//借用构造函数继承方法
Person.call(this,name,age)
}
Man.prototype=inherit(Person.prototype);
//原类式继承方法
function inherit(a){
//创建一个临时构造函数
function F(){
}
// 将传入的对象作为这个构造函数的原型
F.prototype=a;
// 返回临时否则函数的实例
return new F();
}
let m =new Man("litingchen",99)
</script>
</html>