Six inheritance js

1, the prototype inheritance chain

// by inheritance to create their own prototype out of another object constructor, all the properties will be inherited objects are present in __proto__ property.

// Disadvantages:

// 1) a plurality of prototype chain inherited attributes point to the same reference example, a change will affect the properties of another example.

// If it is a reference type, inheritance is an address, then the address and references cited address the parent object of the same sub-prototype

// 2) may not pass

// Because it is the prototype inheritance can not pass parameters

// 3) single inheritance

           function Father(){

              this.FatherName = "father's constructor";

           }

           Father.prototype.age = 40;

           function Son(){

              this.SonNname = "Son's constructor";

           }

           // create out of the inheritance Father Son prototype object, the equivalent inherits everything from the Father, while all properties in the presence Son__proto__

           Son.prototype = new Father();

           Son.prototype.getSubValue = false;

           Son.prototype.age1 = 20;

           var example =new Son();

           console.log(example.age);

 

 

 

2, to borrow the constructor inheritance

    Use call () and apply () constructor of the parent class into subclasses function, using the parent class constructor to enhance the instances of subclasses, equivalent to a copy of the instance of the parent class subclasses

    This method is () apply (), to completely copy a subclass, so any subclass of the operation will not affect the parent class by call

       Disadvantages:

           Examples can inherit attributes and methods of the parent class, the prototype can not inherit the content of

           Can not achieve multiplexing constructor, a copy of each sub-class has a parent class instance of the function, performance impact, bloated code

           function Father(name){

              this.name = name;

              this.colors = ["red"];

           }

           Father.prototype.sex = 1;

           function Son(name,age){

              Father.call (this, name); // call using this point will change this point to the parent class name, such a method to put the Father into the subclasses

              this.age = age;

           }

           var obj = new Son ( "Joe Smith", 25);

           obj.colors.push ( "write"); // reference type does not change the contents of the parent class

           console.log(obj.colors);// [red write]

           var obj2= new Son("jike",10);

           console.log(obj2.colors);//[red]

           console.log (obj2.sex); // undefined content can not be inherited to the parent class prototype

 

 

3, the combination of inheritance

The prototype chain // Constructors inheritance and inheritance of these two modes are combined bit, configured by calling the parent class, the class inherits the properties of the parent parameter passing and retained, then the instance of the parent class as a child class prototype realization function of the complex use.

           Disadvantages:

               Examples of properties of the parent class, i.e., a method exists in the instance of a subclass, are also present in the prototype subclass, accounting memory

              But also inherited the above advantages and disadvantages,

              A reference to a data type, its operation subclass instance will affect the parent class

           Advantages: either the inherited method Prototype class of the parent, reference may be transmitted

           function Father(name){

              this.name = name;

              this.colors = ["red", "blue", "green"];

           }

           Father.prototype.sayName=function(){

              console.log(this.name);

           }

           function Son(name,age){

              // constructor inheritance

               Father.call(this,name);

              this.age = age;

           }

           // prototype chain to inherit

           Son.prototype = new Father();

           var obj = new Son("jike",25);

           console.log(obj);

           obj.sayName (); // jike can call methods on the prototype parent class

           obj.colors.push("write");

           console.log(obj.colors);// ["red", "blue", "green", "write"]

           var obj2 = new Son("mike",30);

           console.log(obj.colors);//["red", "blue", "green", "write"]

 

 

 

4. prototypal inheritance

// Focus: Packaging an object with a function, and then return to the calling function, this function becomes strength or object is free to add attributes object.create () is the principle.

// Features: Similar to copy an object, use the function to wrap

// Disadvantages: 1. all instances inherit property on the prototype method

// 2. reuse can not be achieved. (Property of the new instance is added later)

              function content (obj) {// a function package, and the carrier output objects for prototype inheritance

                  function F () {// create a constructor, while there is produced a prototype

                    

                  }

                  F.prototype = obj; // constructor prototype inherits obj obj passed as a parameter, an object

                  return new F (); // returns a new instance of the use of this function to create

              }

              var obj = new Object (); // obj is an object

              obj.name = "jike"; // attribute to the object obj

              obj.colors = ["red","blue"];

              obj.__proto__.age=[1,2,3,4];

              var obj2 = content (obj); // call the inherited function, and to inherit the object passed to the function inside

              console.log(obj);

              obj2.colors.push("write");

              obj2.age.push(10);

              console.log(obj2);

              console.log(obj2.age);

              console.log(obj);

              var obj3 = Object.create(obj);

              console.log(obj3);

              var obj4 = content(obj);

              console.log(obj4);

// and prototype inheritance chain, same as the prototype object itself inherits a complete object, it can be said that an object as its own prototype object, as a reference to the type of data inheritance is a reference address, a sub-instance reference to the type of data to operate it, all instances will be affected.

Guess you like

Origin www.cnblogs.com/Godfather-twq/p/11494265.html