JS继承--总结

先说一个有关面向对象的小知识点有助于大家对后面理解

1.每个构造函数都有一个原型对象(prototype).
//即每一个构造函数有一个prototype属性,prototype指向原型对象
//原型对象的所有属性和方法都会被构造函数的实例继承.
2.原型对象(prototype)都包含一个构造函数指针(constructor).
//prototype就是通过constructor指向Object的.
3.实例都包含一个指向原型对象的内部指针(_proto_).
//_proto_是查找函数或对象的原型链的方式.
这些都是为了让大家稍微有一些了解,ok,进入正文。
首先,本篇关于JS继承的,我把关于JS继承分为3种方法和两种优化:

1.盗用构造函数继承

2.原型链式继承

3.组合式继承

组合式继承中包含了两个优化

方法1:盗用构造函数继承

function Father1(){
            this.father_Property1 = "父类属性1"
            this.father_Property2 = "父类属性2"
        }
        function Son1(){
            Father1.call(this);//或apply
        }
        new Son1().father_Property1;
        new Son1().father_Property2;
先看第五行,这句话的意思,大家就可以理解为,Son1把Father1叫了过来,对他说:你就是我爸爸!我要继承你的属性!没错,大家就可以简单粗暴的这样理解,很通俗易懂。第六行,子类自己定义了一个属于自己的属性(没啥用~)
    console.log(new Son1().father_Property1)//父类属性1
    console.log(new Son1().father_Property2)//父类属性2
到这里都没问题,子类Son1成功继承了父类Father1中的父类属性1和父类属性2
在接着往下看
 Father1.prototype.father_Property3 = "父类原型上的属性3"
        Father1.prototype.father_Property4 = function(){
            console.log("父类原型上的属性4")
        }
        new Son1().father_Property3;
        new Son1().father_Property4;

        console.log(new Son1().father_Property3)//undefined
        console.log(new Son1().father_Property4)//undefined
到这里就出问题了,Son1并没有继承Father1原型对象上的属性3、4。
我们可以这样理解,不在原型链上的父类属性也就是我们之前可以继承的父类属性1、2,他就相当于父类的肤色,身高,他的子类是可以继承的。但是!!!父类原型(重点是父类原型)上的属性3、4,就相当于父类的第三个孩子和第四个孩子,那么第一个孩子能管第三个孩子和第四个孩子叫儿子吗?
答案显而易见,所以用构造函数实现继承时,子类无法继承父类原型上的属性。

方法2:原型链式继承

function Father1(){
            this.father_Property1 = "父类属性1"
            this.father_Property2 = "父类属性2"
        }
        function Son1(){
           
        }
为了让大家更容易理解,我还是用的Father1和Son1,大家可以发现,原型链继承与构造函数继承很明显的区别,就是这次,我们在Son1里面没有“认爹”这一步,接着看:
 Son1.prototype = new Father1()
        Father1.prototype.father_Property3 = "父类属性3"
        console.log(new Son1().father_Property3)//"父类属性3"
我们直接把Son1的原型对象指向Father1,之后虽然Father1又新定义了属性3,但是我们通过Son1调用之后还是可以使用,这就是我们和第一种方法最明显的优点,就是即使是在父类原型上定义的属性,作为子类,我们依然可以调用!
第二种方法还是有问题
如下
        var s1 = new Son1()
        s1.father_Property1.push("新推入的属性")
        console.log(s1.father_Property1) ["父类属性10", "父类属性11", "父类属性12", "新推入的属性"]
        
        var s2 =new Son1();
        console.log(s2.father_Property1) ["父类属性10", "父类属性11", "父类属性12", "新推入的属性"]
(提个小知识点,就是.push只能往数组里面推入数据,而不能往字符串里面推入数据。)
可以看到我们只单单向S1调用的父类属性1中推入了一个新数据,并没有对S2进行任何操作,但是当我们打印S2的时候,他也被推入了一个新的数据,也就是说,原型链继承方法虽然可以解决“子类无法继承父类原型上的属性”这个问题,但是却出现了他可以更改父类原型问题。这是因为数组相当于是引用类型,保存在原型上,而name等属性属于原始类型,保存在实例中,每个实例都用不同的属性,互不影响,也就是说,只有引用值类型会影响原型链继承。

方法3:组合式继承

 function Father1(){
            this.father_Property1 = ["父类属性01","父类属性02","父类属性03"]
            this.father_Property2 = "父类属性2"
        }
        function Son1(){
            Father1.call(this);//或apply
        }
        Son1.prototype = new Father1();
Father1.call(this);
Son1.prototype = new Father1();
组合式继承,把我们以上两种继承方法的有点结合起来(也“认爹”了,也指定原型对象了)
        var s1 = new Son1()
        s1.father_Property1.push("新推入的属性")
        console.log(s1.father_Property1)["父类属性01", "父类属性02", "父类属性03", "新推入的属性"]
        
        var s2 =new Son1();
        console.log(s2.father_Property1) ["父类属性01", "父类属性02", "父类属性03"]
ok,问题解决
接下来进行优化
我们现在用的这种方法,父类的构造函数是被执行了两次的,第一次:Son1.prototype = new Father1();第二次:实例化的时候会被执行

优化1:

function Father1(){
            this.father_Property1 = ["父类属性01","父类属性02","父类属性03"]
            this.father_Property2 = "父类属性2"
        }
        function Son1(){
            Father1.call(this);//或apply
        }
        Son1.prototype = Father1.prototype;
Son1.prototype = new prototype();直接把父类的原型对象赋给子类的原型对象,这样,父类的构造函数就仅仅被执行了一次,优化OK~
还有一个问题,如下
        var s1 =new Son1();
        var s2 =new Son1();
        console.log(s1 instanceof Son1) //true
        console.log(s1 instanceof Father1) //true
我们要想区分s1到底是由Son1实例化还是由Father1实例化,运用instanceof后发现全是true,所以我们无法判断到底是由谁进行实例化的,换一种方法试验一下:
console.log(s1.constructor.name); //Father1
凉凉,明明由Son1实例化,却指向了Father1。
这是因为Son1.prototype = Father1.prototype;不仅仅将Son1继承了Father1,同事还使Son1的实例化对象也指向了Father1。这不是我们想看到的。

优化2:最终优化:

function Father1(){
            this.father_Property1 = ["父类属性01","父类属性02","父类属性03"]
            this.father_Property2 = "父类属性2"
        }
        function Son1(){
            Father1.call(this);//或apply
        }
        Son1.prototype = Object.create(Father1.prototype);
        Son1.prototype.constructor = Son1;

Object.create是一种创建对象的方式
1.方法内部定义一个新的空对象obj
2.将obj._proto _的对象指向传入的参数proto
3.返回一个新的对象

        var s1 =new Son1();
        var s2 =new Son1();
        console.log(s1 instanceof Son1) //true
        console.log(s1 instanceof Father1) //true
        console.log(s1.constructor.name); //Son1
Son1.prototype.constructor = Son1;是将之前的错误强行指回来!
所有的问题都解决了。

你可能感兴趣的:(JS继承--总结)