<script>
/*
先考虑父级,再考虑子级
1、父级:
人公共的属性和方法有哪些,做成构造函数进行封装
注意:一般公共的属性写到构造函数内部,公共的方法挂载到构造函数的原型对象上
2、子级:
男人继承人类的属性和方法,之后创建自己独有的属性和方法
女人同理
注意:因为对象覆盖了原型对象,所以再把constructor指回当前的构造函数
Man.prototype = new Person();
Man.prototype.constructor = Man;
*/
function Person() {
this.eyes = 2;
}
Person.prototype.eat = function () {
console.log("吃");
}
//function Man() {}
function Woman() {}
// Man.prototype = new Person();
// Man.prototype.constructor = Man;
class Man extends Person {
}
Woman.prototype = new Person();
Woman.prototype.constructor = Woman;
Woman.prototype.baby = function () {
console.log("生");
}
const m = new Man();
const w = new Woman();
console.log(m);
// m.baby(); // 报错 提示没有m.baby这个方法
w.baby(); // 生
</script>
18、js - 面试 - 原型继承
于 2023-06-06 14:50:04 首次发布