重温 ES 5 的继承
1. es5 创建类
function Person () {
this.name = '张三';
this.age = 20;
}
var p = new Person();
console.log(p.name);
console.log(p.age);
2. 构造函数和原型链里面增加方法
function Person () {
this.name = '张三';
this.age = 20;
this.run = function () {
return `${this.name}正在跑步`;
}
}
Person.prototype.sex = '男';
Person.prototype.work = function () {
return `${this.name}正在工作`;
}
var p = new Person();
console.log(p.run());
console.log(p.sex);
console.log(p.work());
3. 静态方法
function Person () {
this.name = '张三';
this.age = 20;
this.run = function () {
return `${this.name}正在跑步`;
}
}
Person.eat = function () {
return '吃东西';
}
console.log(Person.eat());
var p = new Person()
4. es5 对象冒充实现继承
function Person () {
this.name = '张三';
this.age = 20;
this.run = function () {
return `${this.name}正在运动`;
}
}
Person.prototype.sex = '男';
Person.prototype.work = function () {
return `${this.name}正在工作`;
}
function Programmer () {
Person.call(this);
}
var p = new Programmer();
console.log(p.run());
console.log(p.name);
console.log(p.sex);
- 【1】对象冒充继承可以继承构造函数里面的属性和方法
5. es5 原型链继承
function Person () {
this.name = '张三';
this.age = 20;
this.run = function () {
return `${this.name}正在运动`;
}
}
Person.prototype.sex = '男';
Person.prototype.work = function () {
return `${this.name}正在工作`;
}
function Programmer () {}
Programmer.prototype = new Person();
var p = new Programmer();
console.log(p.name);
console.log(p.run());
console.log(p.sex);
console.log(p.work());
- 【1】原型链继承可以实现继承构造函数内的属性和方法
- 【2】原型链继承可以实现继承构造函数原型链上的属性和方法
6. 原型链继承有什么问题?
function Person (name, age) {
this.name = name;
this.age = age;
this.run = function () {
return `${this.name}正在运动`;
}
}
Person.prototype.sex = '男';
Person.prototype.work = function () {
return `${this.name}正在工作`;
}
var p = new Person('张三', 20);
console.log(p.name);
console.log(p.sex);
console.log(p.run());
console.log(p.work());
function Programmer (name, age) {};
Programmer.prototype = new Person();
var p2 = new Programmer('李四', 22);
console.log(p2.name);
console.log(p2.sex);
console.log(p2.run());
console.log(p2.work());
7. 对象冒充继承再发现
function Person (name) {
this.name = name;
this.run = function () {
return `${this.name}正在运动`;
}
}
function Programmer (name) {
Person.call(this, name);
}
var p = new Programmer('张三');
console.log(p.name);
console.log(p.run());
- 【1】对象冒充继承在实例化子类的时候可以向父类传参
8. 原型链 + 对象冒充实现继承
function Person (name) {
this.name = name;
this.run = function () {
return `${this.name}正在运动`;
}
}
Person.prototype.sex = '男';
Person.prototype.work = function () {
return `${this.name}正在工作`;
}
function Programmer (name) {
Person.call(this, name);
}
Programmer.prototype = new Person();
var p = new Programmer('张三');
console.log(p.name);
console.log(p.sex);
console.log(p.run());
console.log(p.work());
9. 原型链 + 对象冒充实现继承的另一种方式
function Person (name) {
this.name = name;
this.run = function () {
return `${this.name}正在运动`;
}
}
Person.prototype.sex = '男';
Person.prototype.work = function () {
return `${this.name}正在工作`;
}
function Programmer (name) {
Person.call(this, name);
}
Programmer.prototype = Person.prototype;
var p = new Programmer('张三');
console.log(p.name);
console.log(p.sex);
console.log(p.run());
console.log(p.work());