JS is an object-oriented language, so there are some object-oriented ways----------such as inheritance. Next introduce 5 kinds of JS inheritance way. Note: The function in JS is actually an object, and the function name is a reference to a function object.
1. Using the call method to change the function context to implement inheritance, the principle is to change the function inside the function context this, so that it points to the specific object of the function
The specific code is as follows
Father.call (child,child.getname) means that using Father objects instead of child objects, child has all the properties and methods of Father, the child object can directly invoke the Father method and properties.
2. Use the Apply method to change the function context to implement inheritance, the principle is to change the function inside the function context this, so that it points to the specific object of the function
3. Implement inheritance in the form of a prototype chain, with the principle that the subclass prototype object is directed to an instance of the parent class to implement inheritance, that is, to rewrite the prototype of the class
This way the class has the properties and methods of the parent class, perfectly inheriting
4. Using an object to impersonate the implementation of inheritance (the implementation can implement multiple inheritance) principle: Let the parent class constructor become a method of the subclass, and then call the method of the subclass, through the This keyword to all the properties and methods to assign values
Subclasses have methods and properties that can see that subclasses inherit the methods and properties of the parent class
5. Implementing inheritance with Mixed mode
5 ways in which JavaScript implements inheritance