5 ways in which JavaScript implements inheritance

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.