继承,是子类继承父类的特征和行为,使得子类对象具备父类的实例域和方法。 继承是面向对象编程中,不可或缺的一部分。
html
减小代码冗余
父类能够为子类提供通用的属性,而没必要由于增长功能,而逐个修改子类的属性代码复用
同上代码易于管理和扩展
子类在父类基础上,能够实现本身的独特功能耦合度高
若是修改父类代码,将影响全部继承于它的子类影响性能
子类继承于父类的数据成员,有些是没有使用价值的。可是,在实例化的时候,已经分配了内存。因此,在必定程度上影响程序性能。1:首先定义一个父类前端
// 定义一个动物类
function Animal (name) {
// 属性
this.name = name || 'Animal';
// 实例方法
this.sleep = function(){
console.log(this.name + '正在睡觉!');
}
}
// 原型方法
Animal.prototype.eat = function(food) {
console.log(this.name + '正在吃:' + food);
};
复制代码
注:不一样继承方式可能只继承父类的属性和实例方法,也可能只继承原型的方法,也可能二者都继承(看完下面的几种继承方式后应该就明白了)编程
核心: 将父类的实例(*)做为子类的原型bash
function Cat(){
}
Cat.prototype = new Animal();
Cat.prototype.name = 'cat';
var cat = new Cat();
console.log(cat.name);
console.log(cat.eat('fish'));
console.log(cat.sleep());
console.log(cat instanceof Animal); //true
console.log(cat instanceof Cat); //true
复制代码
注:若是只是将子类的原型对象指向父类的原型对象(不是实例),则只继承原型的方法函数
function Lion(){
}
Lion.prototype = Animal.prototype
console.log(cat.eat('fish'));
console.log(cat.sleep()); // Uncaught TypeError: lion.sleep is not a function
复制代码
特色:post
缺点:性能
new Animal()
这样的语句以后执行,不能放到构造器中核心:使用父类的构造函数来加强子类实例,等因而复制父类的实例属性给子类(没用到原型)
学习
function Cat(name){
Animal.call(this);
this.name = name || 'Tom';
}
// Test Code
var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
console.log(cat.eat()); // Uncaught TypeError: cat.eat is not a function
console.log(cat instanceof Animal); // false
console.log(cat instanceof Cat); // true复制代码
特色:ui
缺点:this
核心:为父类实例添加新特性,做为子类实例返回
function Cat(name){
var instance = new Animal();
instance.name = name || 'Tom';
return instance;
}
// Test Code
var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
console.log(cat instanceof Animal); // true
console.log(cat instanceof Cat); // false
复制代码
特色:
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);
console.log(cat.sleep());
console.log(cat instanceof Animal); // false
console.log(cat instanceof Cat); // true复制代码
特色:
缺点:
核心:经过调用父类构造,继承父类的属性并保留传参的优势,而后经过将父类实例做为子类原型,实现函数复用
function Cat(name){
Animal.call(this);
//此处能够调用相似Animal的多个父类构造函数,实现多继承
this.name = name || 'Tom';
}
Cat.prototype = new Animal();
Cat.prototype.constructor = Cat;
// Test Code
var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
console.log(cat instanceof Animal); // true
console.log(cat instanceof Cat); // true复制代码
特色:
缺点:
核心:经过寄生方式,砍掉父类的实例属性,这样,在调用两次父类的构造的时候,就不会初始化两次实例方法/属性,避免的组合继承的缺点
function Cat(name){
Animal.call(this);
this.name = name || 'Tom';
}
(function(){
// 建立一个没有实例方法的类
var Super = function(){};
Super.prototype = Animal.prototype;
//将实例做为子类的原型(new Super()的实例不包含Animal的属性和实例方法,只包含Animal的原型方法,
因此比第五种方法少初始化了一次Animal的属性和实例方法)
Cat.prototype = new Super();
})();
// Test Code
var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
console.log(cat instanceof Animal); // true
console.log(cat instanceof Cat); //true复制代码
参考:https://juejin.im/post/5b5f3e9c5188257bcc167bc6
https://www.cnblogs.com/humin/p/4556820.html
前端小白第一次写 主要但愿帮助本身更好了理解和学习 能对你们有帮助的话也是很是欣慰,谢谢参考的两位的文章
但愿多提意见,,,可是勿喷,谢谢你们~~