JS作为面向对象的弱类型语言,继承也是其非常强大的特性之一。
既然要实现继承,那么首先我们得有一个父类,代码如下:
// 定义一个动物类
function Animal (name) {
// 属性
this.name = name || 'Animal';
// 实例方法
this.sleep = function(){
console.log(this.name + '正在睡觉!');
}
}
// 原型方法
Animal.prototype.eat = function(food) {
console.log(this.name + '正在吃:' + food);
};
核心:将父类的实例作为子类的原型
function Cat() {
}
Cat.prototype = new Animal();
Cat.prototype.name = 'cat';
// Test Code
var cat = new Cat();
console.log(cat.name);
cat.eat('fish'); //cat正在吃fish
cat.sleep(); //cat正在睡觉。。。
console.log(cat instanceof Animal); //true
console.log(cat instanceof Cat); //true
特点:
缺点:
Cat.prototype = new Animal();
这样的语句之后执行。核心:使用父类的构造函数来增强子类实例,等于是复制父类的实例属性给子类(没用到原型)
function Cat2(name) {
Animal.call(this);
this.name = name || 'Tom';
}
// Test Code
var cat2 = new Cat2();
console.log(cat2.name); //Tom
cat2.sleep() //Tom正在睡觉。。。
console.log(cat2 instanceof Cat2); //true
console.log(cat2 instanceof Animal); //false
特点:
缺点:
核心:为父类实例添加新特性,作为子类实例返回
function Cat3(name) {
var instance = new Animal();
instance.name = name || 'Tom';
return instance;
}
// Test Code
var cat3 = new Cat3();
console.log(cat3.name);
cat3.sleep();
cat3.eat('meat');
console.log(cat3 instanceof Cat3); //false
console.log(cat3 instanceof Animal); //true
特点:
new 子类()
还是 子类()
,返回的对象具有相同的效果缺点:
function Cat(name) {
var animal = new Animal();
for (var p in animal) {
Cat.prototype[p] = animal[p];
}
Cat.prototype.name = name || 'Tom';
}
// Test Code
var cat = new Cat();
console.log(cat.name);
cat.sleep();
cat.eat('bread');
console.log(cat instanceof Cat); //true
console.log(cat instanceof Animal); //false
特点:
缺点:
核心:通过调用父类构造函数,继承父类的属性并保留传参的优点,然后通过将父类实例作为子类原型,实现函数复用。
function Cat(name) {
Animal.call(this);
this.name = name || "Tom";
}
Cat.prototype = new Animal();
Cat.prototype.constructor = Cat;
// Test Code
var cat = new Cat();
console.log(cat.name);
cat.sleep();
cat.eat('bread');
console.log(cat instanceof Cat); //true
console.log(cat instanceof Animal); //true
特点:
缺点:
核心:通过寄生方式,砍掉父类的实例属性,这样,在调用两次父类的构造的时候,就不会初始化两次实例方法/属性,避免的组合继承的缺点。
function Cat(name) {
Animal.call(this);
this.name = name || "Tom";
}
(function () {
// 创建一个没有实例方法的类
var Super = function () {
}
Super.prototype = Animal.prototype;
// 将实例作为子类的原型
Cat.prototype = new Super();
})();
// 修复构造函数
Cat.prototype.constructor = Cat;
// Test Code
var cat = new Cat();
console.log(cat.name);
cat.sleep();
cat.eat('bread');
console.log(cat instanceof Cat); //true
console.log(cat instanceof Animal); //true
特点:
缺点:
首先使用es6语法把代码简化如下:
class Animal2 {
constructor (name, food) {
this.name = name || "Animal";
this.food = food;
this.sleep = () => console.log(this.name + '正在睡觉。。。');
}
eat(){
// console.log(this.name + '正在吃' + this.food);
return this.name + '正在吃' + this.food;
}
}
然后让Reptile类继承Animal2
class Reptile extends Animal2 {
constructor (name, food, habit){
super(name, food);
this.habit = habit;
}
printHabit() {
console.log(this.habit);
}
}
// Test Code
let cat = new Reptile('cat', 'fish', 'sleep');
cat.printHabit(); //sleep
console.log(cat.eat()); //cat正在吃fish
console.log(cat instanceof Reptile); //true
console.log(cat instanceof Animal2); //true
javascript继承的7种方法(本人目前只了解这几种继承方式)
本文若有疏漏的地方,请在评论区留言,欢迎各位批评指正。