关于使用原型实现类式继承
对于JS,在ES6中出现了类,而在ES6之前我门要用原型实现类式继承,方式如下。首先 创建一个父类函数
function fatherclass() {
this.age = 10
this.num = 20
}
//需要在父类的原型上添加方法 利用原型实现方法继承
fatherclass.prototype.move = function (x, y) {
this.age += x
this.num += y
console.log(this.age, this.num)
}
//利用call方法改变this 指向 实现属性的继承
function sonclass() {
fatherclass.call(this)
}
//用object.creat方法 copy父类原型对象
sonclass.prototype = Object.create(fatherclass.prototype)
//创建在子类原型对象上创建自己的构造函数(当孙子类继承子类时可直接继承子类构造函数)
sonclass.prototype.constructor = sonclass
console.log(sonclass.prototype)
var apk = new sonclass()
apk.move(1, 2)
以上方法就是 实现类式继承
如有错误多多指教