js inherited six ways

Want to inherit, it is necessary to provide a parent (who inherited providing inherited properties)
    
  First, the prototype chain to inherit
    
    focus: Let a new instance of the prototype equals instance of the parent class.
    Features: 1, examples of inheritable attributes: Attribute constructors example, the parent class constructor properties, the properties of the parent class prototype. (A new instance of the class does not inherit the property of the parent instance!)
    Disadvantages: 1, a new instance can not be a reference to the parent class constructor pass.
       2, single inheritance.
       3, all new instances will share the parent class instance attributes. (The prototype attributes are shared, a modified example of the prototype attributes, another example of the prototype property will be modified!)
  
  Second, the constructor Inheritance borrow
    
    Key: with .call () and .apply () constructor parent function function is integrated into subclasses (made from the parent class function performed in the sub-class functions (copy))
    characteristics: 1, only inherits the properties of the parent class constructor, not the parent class inherit the properties of the prototype.
       2, to solve the shortcomings of the prototype chain to inherit 1,2,3.
       3, a plurality of constructors can inherit attributes (call s).
       4, in the example sub Examples parameter passing to a parent.
    Disadvantages: 1, can only inherit the properties of the parent class constructor.
       2, can not achieve multiplexing constructor. (Each with every time recall)
       , there is a copy of the parent class constructor each new instance 3, bloated.

  Third, the combination of inheritance (inheritance chain and prototype composition borrow constructors inherited) (common)
    
    Key: combines the advantages of both modes, parameters, and multiplexing transmission
    characteristics: 1, can inherit the properties of the parent class prototype parameters can be transferred, reusable.
       2, the attributes of each new instance constructor is private introduced.
    Disadvantages: called twice parent class constructor (memory consumption), constructor of the subclass will replace the parent class constructor in the prototype.

  Fourth, the prototypal inheritance
    
    emphasis: packaging an object with a function, and then return to the calling function, this function becomes free to add a property or an instance of an object. object.create () is the principle.
    Features: Similar to copy an object, use the function to wrap.
    Disadvantages: 1, all instances will inherit property on the prototype.
       2, can not achieve multiplexing. (A new instance of attributes are added later)
  
  V. Parasitic inheritance
    
    focus: that is to set out a prototype formula inherited a shell.
    Pros: not created a custom type, because it is only a shell set up to return the object (this), the new object this function has become a matter of course created.
    Cons: useless to prototype, can not be reused.
    
  Six, combined parasitic inheritance (common)
    Parasitic: Returns the object and then calls the function in
    combination: a prototype function is equal to another example. 2, with the introduction of a function apply or call another constructor, mass participation can 
    
    focus: Fixed combination of inherited problems

    inherited this knowledge point is not so much the object of inheritance, function more like the usage of function, how to do a function reuse, I think a combination of these and the use of inheritance is the same. Several inherited the above methods can repair their shortcomings manual, but the manual is more than a repair becomes another mode of inheritance.
    The study focused on inheritance patterns are learning their thoughts, or you'll examples in books on coding time, will feel that obviously can inherit directly why engage in such trouble. Like prototypal inheritance function to copy it with a copy of the internal object, so not only can inherit properties inside the object, but also the function (object returns an internal source object) free to call, add attributes to them, change parameters on you can change the prototype object, and these new attributes do not affect each other.

 

Guess you like

Origin www.cnblogs.com/ranyonsue/p/11201730.html