面向对象编程很重要的一个方面,就是对象的继承。A
对象经过继承B
对象,就能直接拥有B
对象的全部属性和方法。这对于代码的复用是很是有用的。传统上,JavaScript
语言的继承不经过class
,须要使用原型机制或者用applay
和call
方法实现。ES6
引入了class
语法,则出现了基于class
的继承。编程
为何要使用继承?继承解决了什么问题?这里就不卖关子了,直接给出答案。继承是为了解决构造函数的缺陷,解决在同一个构造函数的多个实例之间,没法共享属性,从而形成对系统资源的浪费。让咱们经过下面例子简单来分析一下下。数组
例子1:bash
function Cat (name, color) {
this.name = name;
this.color = color;
}
var cat1 = new Cat('大毛', '白色');
cat1.name // '大毛'
cat1.color // '白色'
复制代码
以上代码中,Cat
函数是一个构造函数,函数内部定义了name
属性和color
属性,全部实例对象(cat1
)都会生成这两个属性。下面咱们再改造下例子1。app
例子2:函数
function Cat(name, color) {
this.name = name;
this.color = color;
this.meow = function () {
console.log('喵喵');
};
}
var cat1 = new Cat('大毛', '白色');
var cat2 = new Cat('二毛', '黑色');
cat1.meow === cat2.meow
// false
复制代码
例子2中cat1
和cat2
是同一个构造函数的两个实例,它们都具备meow
方法。因为meow
方法是生成在每一个实例对象上面,因此两个实例就生成了两次。ok,我想你们到这都很清楚明白。可是,问题来了,每新建一个实例,这里面就会新建一个函数方法,这既没有必要,又浪费系统资源,由于全部meow
方法都是一样的行为,彻底能够共享复用。学习
存在即合理。上面经过例子大概了解继承出现是为了干什么,如今我们得知道它是如何干活的,即继承要如何实现呢?开始以前看看都要哪些方法能够实现继承。ui
call
方法继承applay
方法继承ES6
实现继承将构造函数的原型设置为另外一个构造函数的实例对象,这样就能够继承另外一个原型对象的全部属性和方法,能够继续往上,最终造成原型链。this
子类经过prototype
将全部在父类中经过prototype
追加的属性和方法都追加到子类,从而实现继承。为了让子类继承父类的属性(也包括方法),首先须要定义一个构造函数,而后,将父类的新实例赋值给构造函数原型。具体看下面代码。spa
function parent(){
this.name="garuda";
}
function child(){
this.sex="man"
}
child.prototype=new parent();//核心:子类继承父类,经过原型造成链条
var test=new child();
console.log(test.name);
console.log(test.sex);
复制代码
在js中,被继承的函数称为超类型(父类、基类),继承的函数称为子类型(子类、派生类)。prototype
使用原型继承存在两个问题:一是面量重写原型会中断关系,使用引用类型的原型,二是子类型还没法给超类型传递参数。
为了解决原型中包含引用类型值的问题,开始使用借用构造函数,也叫伪造对象或经典继承
function parent(){
this.name="garuda";
}
function child(){
parent.call(this);//核心:借父类型构造函数加强子类型(传参)
}
var test =new parent();
console.log(test.name);
复制代码
存在的问题就是,全部的类型都只能使用构造函数模式(由于超类型的原型中定义的方法对于子类型不可见),所以方法都在构造函数中定义,函数复用就无从谈起了。
call
方法是Function
类中的方法call
方法的第一个参数的值赋值给类(即方法)中出现的this
,call
方法的第二个参数开始依次赋值给类(即方法)所接受的参数(参数列表)。
function test(str){
alert(this.name + " " + str);
}
var object = new Object();
object.name = "zhangsan";
test.call(object,"langsin");
//此时,第一个参数值object传递给了test类(即方法)中出现的this,
// 而第二个参数"langsin"则赋值给了test类(即方法)的str
function Parent(username){
this.username = username;
this.hello = function(){
alert(this.username);
}
}
function Child(username,password){
Parent.call(this,username);
this.password = password;
this.world = function(){
alert(this.password);
}
}
var parent = new Parent("zhangsan");
var child = new Child("lisi","123456");
parent.hello();
child.hello();
child.world();
复制代码
apply方法接受2个参数,第一个参数与call
方法的第一个参数同样,即赋值给类(即方法)中出现的this
,第二个参数为数组类型,这个数组中的每一个元素依次赋值给类(即方法)所接受的参数(数组参数)。
function Parent(username){
this.username = username;
this.hello = function(){
alert(this.username);
}
}
function Child(username,password){
Parent.apply(this,new Array(username));
this.password = password;
this.world = function(){
alert(this.password);
}
}
var parent = new Parent("zhangsan");
var child = new Child("lisi","123456");
parent.hello();
child.hello();
child.world();
复制代码
也叫伪经典继承,将原型链和借用构造函数的技术组合到一块。使用原型链实现对原型属性和方法的继承,而经过构造函数来实现对实例属性的继承。
function parent(){
this.name="garuda";
}
function borther(){
return this.name;
}
function child(){
parent.call(this)
}
child.prototype=new parent();
var test=new parent();
console.log(test.borther())
复制代码
其实是借用了构造函数,以覆盖的方式,解决了在原型链继承中原型的引用类型属性共享在全部实例中的问题。
ES6
的class
是语法糖,其实质就是函数,而上述用class
实现继承的过程,仍是基于原型链(和ES5
的是否是彻底一致)
// ES6 写法
class Human{
constructor(name){
this.name = name
}
run(){
console.log("我叫"+this.name+",我在跑")
return undefined
}
}
class Man extends Human{ // extends 实现上述继承过程
constructor(name){
super(name) // 调用构造函数:'超类'
this.gender = '男'
}
fight(){
console.log('糊你熊脸')
}
}
复制代码
JS
中的继承关系是很重要的技术知识,在实际开发中常常会用到,不了解的童鞋须要加紧学习理解哦!