<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>完美继承</title>
<script>
function Father(name){
this.name = name;
}
Father.prototype.sayName = function(){
console.log(this.name)
};
function Son(name, age){
// 第一步: 获取父构造函数函数体内的属性
Father.call(this, name)
}
// 第二步: 根据父构造函数原型 创建独立的子构造函数原型, 且要求子构造函数原型的__proto__属性指向子构造函数
Son.prototype = Object.create(Father.prototype);
// 第三步: 将子构造函数原型的constractor指向子构造函数
Son.prototype.constructor = Son;
son1 = new Son("小明", 13);
console.log("子对象的原型指向子构造函数:",son1.__proto__ === Son.prototype);
console.log("子对象的原型指向父构造函数:",son1.__proto__ === Father.prototype);
console.log("打印子对象", son1);
son2 = new Son("小红", 14);
console.log("打印两个子对象的属性",son1, son2);
son1.sayName();
son2.sayName();
// 重新父原型内的方法 ,观察是否会对父原型产生影响
Son.prototype.sayName = function(){
console.log("子原型函数sayName", this.name);
};
// 父实例对象调用sayName
father1 = new Father("老王");
father1.sayName();
// 子实例调用sayName
son1.sayName();
son2.sayName();
</script>
</head>
<body>
</body>
</html>