ES6
中的 class
类 可以通过 extends
关键字实现继承,而这道题,面试官可能更想问的是在 ES5
中如何实现
javascript// 父类: 公共属性和方法
function Person() {
this.name = "yjw"
}
// 父类定义一个吃的方法
Person.prototype.eating = function() {
console.log(this.name + ' is eating')
}
// 子类: 特有属性和方法
function Student() {
this.sno = '001'
}
// Student的原型对象指向一个Person的实例对象per
const per = new Person()
Student.prototype = per
// 子类定义一个学习的方法
Student.prototype.studying = function() {
console.log(this.name + ' is studying')
}
const stu = new Student()
console.log(stu)
console.log(stu.name) // stu对象中没有name属性 会去他的原型对象per上找 per对象上有name属性
stu.eating() // stu对象中没有eating方法 会去他的原型对象per上找per对象上也没eating方法 再往上去per的原型对象上找 per的原型对象上有eating方法
stu.studying()
这种方式总结就是:子类的原型指向父类的一个实例对象
Person
传递参数,因为这个对象是一次性创建的(没办法定制化)javascript// 父类: 公共属性和方法
function Person(name) {
this.name = name
}
// 父类定义一个吃的方法
Person.prototype.eating = function() {
console.log(this.name + ' is eating')
}
// 子类: 特有属性和方法
function Student(name, sno) {
// 借用了父类的构造函数
Person.call(this, name)
this.sno = sno
}
// Student的原型对象指向一个Person的实例对象per
const per = new Person()
Student.prototype = per
// 子类定义一个学习的方法
Student.prototype.studying = function() {
console.log(this.name + ' is studying')
}
借用构造函数继承解决了上面的三个问题。但还是不够完美
javascript// 父类: 公共属性和方法
function Person(name) {
this.name = name
}
// 父类定义一个吃的方法
Person.prototype.eating = function() {
console.log(this.name + ' is eating')
}
// 子类: 特有属性和方法
function Student(name, sno) {
// 借用了父类的构造函数
Person.call(this, name)
this.sno = sno
}
Student.prototype = Object.create(Person.prototype) // 原型式继承 不用new Person()多调用父类构造函数了
Object.defineProperty(Student.prototype, "constructor", {
enumerable: false,
configurable: true,
writable: true,
value: Student
}) // 改构造函数名称
// 子类定义一个学习的方法
Student.prototype.studying = function() {
console.log(this.name + ' is studying')
}
javascript// 上面的 Object.create(Person.prototype) 也可以写成 (兼容性)
Object.setPrototypeOf(Student.prototype, Person.prototype)
// 也可以写成 (兼容性)
function object(o) {
function F() {}
F.prototype = o
return new F()
}
Student.prototype = object(Person.prototype)
本文作者:叶继伟
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!