js继承

最基础的原型链

            function superType () {
                this.property=true;
            }
            superType.prototype.getSuperProperty=function  () {
                return this.property;
            }
            function subType () {
                this.subproperty=false;
            }
            subType.prototype=new superType();
            subType.prototype.getSubProperty=function  () {
                return this.subproperty;
            }
            var a=new subType();
            console.log(a.getSuperProperty(),a.getSubProperty());//true,false

原型和实例的关系

a instanceof object

a instanceof subType

a instanceof superType//都是true


借用构造函数

在继承中,也有遗留的实例和原型函数中的引用问题

如果超类的原型函数中有引用,子类的修改将受到波及

我们现在可以使用一种组合的方法,在子类中定义一个超类,superType.call(this)

            function superType () {
                this.color=["huanggabin","sb"];            
            }
            function subType () {
                superType.call(this);
            }
            superType.prototype=new superType();
            var a=new subType();
            a.color.push("fuck");
            console.log(a.color);
            var b=new subType();
            console.log(b.color);

 如果超类有参数

            function superType (name,age) {
                this.name=name;
                this.age=age;
            }
            function subType () {
                superType.call(this,name,age);
            }


组合继承

组合上面的两种方法:原型链和借用构造函数

可以有自己的引用同时拥有原型链

            function superType (name) {
                this.name=name;
                this.color=["red","yellow"];
            }
            function subType (name,age) {
                superType.call(this,name);
                this.age=age;
            }
            superType.prototype.sayName=function  () {
                console.log(this.name);
            }
            subType.prototype=new superType();
            subType.prototype.sayAge=function  () {
                console.log(this.age);
            }
            var a=new subType("huanggabin",20);
            a.color.push("red");
            console.log(a.color);
            a.sayAge();
            a.sayName();
            var b=new subType("huangsb",20);
            console.log(b.color);
            b.sayAge();
            b.sayName();
        </script>

原型式继承

            function object (o) {
                function f () {
                }
                f.prototype=o;
                return new f();
            }

o作为一个和新对象相似的对象,和原型链有相似,是以一个对象的实例作为超类,引用类型在里面会公用

上面函数可以用Object.create(o)代替

            function Person () {
                this.name="huanggabin";
                this.color=["red","yellow"];
            }
            var a=new Person();
            var b=Object.create(a);
            b.name="sb";
            b.color.push("dddd");
            console.log(a,b);

寄生式继承=原型式继承+工厂模式+寄生构造函数(和工厂差不多那个加强特有的array那种

            function Person (name) {
                this.name=name;
            }
            function createBPerson (b) {
                var a=Object.create(b);
                a.sayName=function  () {
                    console.log(this.name);
                };
                return a;
            }
            var a=new Person("huanggabin");
            var b=createBPerson(a);
            b.sayName();

和原型式继承查不到,就是封装好了的工厂模式


寄生组合式继承

在组合继承很好的情况下,仍然有两次调用超类的构造函数和有一些无用数据,

我们加油了寄生式继承

            function createSub (SubType,SuperType) {
                var a=Object.create(SuperType.prototype);
                a.constructor=SubType;
                SubType.prototype=a;
            }

上面的寄生式继承,帮我们完成了对象的链接

			function SuperType (name) {
				this.name=name;
				this.color=["red","yellow"];
			}
			function SubType (name,age) {
				SuperType.call(this,name);				
				this.age=age;
			}
			SuperType.prototype.sayName=function  () {
				console.log(this.name);
			}
			SubType.prototype.sayAge=function  () {
				console.log(this.age);
			}
			createSub(SubType,SuperType);//不用SubType.prototype=new SuperType();
			var a=new SubType("huanggabin",20);
			console.log(a);

  

原文地址:https://www.cnblogs.com/vhyc/p/5772757.html