JS继承的实现
继承的概念:继承的是对象,对象具有三大特征:(封装、继承、多态),继承的是属性和行为。
要实现继承,首先我们需要有一个父类,代码如下:
function Animal(){
//在其中写关于父类这一类的特征和行为
this.name=null;
this.AnimalColor=null;
this.sex=null;
this.age=null;
this.type=null;
this.sleep=function (){
return this.name+"会睡觉";
}
原型链追加(继承) 给原型方法里面追加属性和行为
Animal.prototype.eat=function (){
return this.name+"会吃饭";
}
定义一个子类对象
function Cat(){
this.catchMouse=function (){
return this.name+"抓老鼠";
}
}
1.原型链继承
原理:将父类的实例作为子类的原型继承
var a=new Animal();//实例对象,实例化父类对象
Cat.prototype=a;
Cat.prototype.speed=function (){
return 5.0;
}
var c=new Cat();//实例化子类对象,此时子类就继承了父类的属性和行为
c.name="小花";
c.type="猫科";
c.AnimalColor="白家黑";
c.age="2";
c.sex="母";
优缺点:
优:简单,易于实现,父类新增属性,行为,子类都能访问到
缺:要给子类追加属性,行为,只能放在实例化语句之后,且无法多继承,无法向构造函数传参。
2.构造函数
原理:将父类的实例复制给子类
先定义一个父类,代码如下:
function Animal(name,color,sex,age,type){
/*动物类有什么特征和行为*/
this.name=name;
this.AnimalColor=color;
this.sex=sex;
this.age=age;
this.type=type;
this.sleep=function (){
return this.name+"会睡觉";
}
}
Animal.prototype.solo=function (){
return "哈哈"
}
function rou(name){
this.name=name;
this.eat=function (){
return this.name+"吃肉";
}
}
定义一个子类
function Cat(name,color,sex,age,type){
/* call apply 在args上有区别 call 序列传参 apply 为数组传参,即写成数组*/
Animal.call(this,name,color,sex,age,type);
/* Animal.apply(this,[1,2,3,4,5]);*/
/*this.name=name;*/
rou.apply(this,[name]);
}
var c=new Cat("小猫","白色","母",2,"猫");//实例子类
优缺点:
优:可以实现多继承
缺:无法继承父类新增的属性和行为
3.实例继承
原理:子类直接指向父类
先去定义一个父类对象
function Animal(){
/*动物类有什么特征和行为*/
this.name=null;
this.AnimalColor=null;
this.sex=null;
this.age=null;
this.type=null;
this.sleep=function (){
return this.name+"会睡觉";
}
}
Animal.prototype.speed=function (){
return 5.0;
}
定义一个子类
function Cat(){
var a=new Animal();//a直接实例到了父类,且返回给了子类,相当于子类直接指向父类
a.name="小猫";
return a;
}
var c=new Cat();
优缺点:
优:不限制调用方式
缺:实例是父类的实例,不是子类的实例。
4.组合继承
原理:组合原型链和构造继承的优点
先去定义一个父类
function Animal(){
/*动物类有什么特征和行为*/
this.name=null;
this.AnimalColor=null;
this.sex=null;
this.age=null;
this.type=null;
this.sleep=function (){
return this.name+"会睡觉";
}
}
Animal.prototype.speed=function (){
return 5.0;
}
function rou(){
this.eat=function (){
return "吃肉";
}
}
//定义一个子类对象
function Cat(){
Animal.call(this);
rou.call(this);
}
Cat.prototype=new Animal();
var c=new Cat();
优缺点:
优:二者的优点结合。
缺:生成了两个实例,消耗内存。
5.单例模式
单例模式的思路:一个类能返回一个对象的引用(并且永远是同一个)和一个获得该实例的方法(静态方法,通常使用 getInstance 名称)。那么当我们调用这个方法时,如果类持有的引用不为空就返回该引用,否者就创建该类的实例,并且将实例引用赋值给该类保持的那个引用再返回。同时将该类的构造函数定义为私有方法,避免其他函数使用该构造函数来实例化对象,只通过该类的静态方法来得到该类的唯一实例。
var data=(function (){ //唯一的对象
function student(){
this.name="小明"
}
function getStuinfo(){
var s=new student();
return s;
}
return {
info:getStuinfo()
}
})();
console.log(data);