【发布时间】:2016-11-14 05:40:01
【问题描述】:
如果我们在每个子函数中使用this调用构造函数方法,可以直接继承父属性,为什么还要设置原型对象进行继承?
function Employee() {
this.name = "";
this.dept = "general";
}
function Manager() {
Employee.call(this);
this.reports = [];
}
Manager.prototype = Object.create(Employee.prototype);
即使我们不将Manager的原型设置为Employee,我们也可以使用继承。
【问题讨论】:
-
试试
new Manager() instanceof Employee没有它,或者尝试在Employee的原型上添加东西。