SE5寄生组合式继承
时间:2023-4-23 00:36 作者:小诸葛 分类: JavaScript 正在检查是否收录...
// ES5继承三步骤:
// 1).在子类构造函数中调用父类构造函数,让子类实例化出来的对象拥有父类的属性
// 2).让子类的原型成为父类的实例,让子类实例化出来的对象拥有父类的方法
// 3).找回丢失的构造函数
// 定义父类
function User(username, password) {
this.username = username;
this.password = password;
}
User.prototype.login = function () {
console.log(this.username + "登录成功!");
}
// 定义子类
function Student(uid, ...arr) {
this.uid = uid;
// 1)
User.apply(this, arr);
}
Student.prototype = Object.create(User.prototype)
Student.prototype.constructor = Student
let p1 = new Student("001", "小明", "151568")
console.log(p1);
p1.login()
//寄生组合式继承原理
function create(obj) {
function F() {
};
F.prototype = obj;
return new F();
}
推荐阅读:
扫描二维码,在手机上阅读