There are 5 ways to implement JavaScript inheritance

Source: Internet
Author: User

1. Object Impersonation

function Parent (username) {    this.username = Username;    This.hello = function () {        alert (this.username);    }}    function Child (Username,password) {    ///By appending the properties and methods of the parent to the child through the following 3 line implementations to implement the integration implementation inheritance    //1. This.method is a temporary property and points to the object that the parent points to,    //2. Executes the This.method method, which is the object function//3 that the parent points to    . Destroys the This.method attribute, which means that at this point the child has all the properties and methods of the Parent This.method = Parent;this.method (username);//Key row Delete This.method ; this.password = password;    This.world = function () {        alert (This.password);    }} var parent = new Parent ("Zhangsan"), var child = new Child ("Lisi", "123456");p Arent.hello (); Child.hello (); Child.world (); 2. Inherit the second way: Call () method call method is a method in the function class The value of the first parameter of the all method is assigned to the class (that is, the method) in the second argument of the ThisCall method that occurs in order to assign a value to the class (that is, the method) the parameters accepted

3. Third Way of inheritance: Apply () method

The Apply method accepts 2 parameters

A. The first parameter is the same as the first parameter of the call method, which is assigned to the this that appears in the class (that is, the method)

B. The second parameter is an array type, in which each element in the sequence is assigned to a parameter that is accepted by the class (that is, the method)

4. Fourth way: The prototype chain, that is, the subclass prototype all the attributes and methods appended by prototype in the parent class to child, thus implementing the inheritance.

5. Fifth way of inheritance: Hybrid mode

Mixed call mode, prototype chain way.

  

There are 5 ways to implement JavaScript 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.