Introduction to the implementation of the JavaScript Inheritance Mechanism

Source: Internet
Author: User
Object impersonating method implementation: [javascript] functionHuman () {& lt; spanstyle & quot; white-space: pre & quot; & gt; & lt;/span & gt; // define the Human class this. species & quot; Human & quot;} functionSex (sex) {& lt; spa

Object impersonating method implementation:

[Javascript]
Function Human () {// defines the Human class
This. species = "Human ";
}
Function Sex {// defines the sex class
This. sex = sex;
}
Function Chinese (name, sex ){
This. name = name;
This. newMethod1 = Human; // object impersonate, pointing to the Human object
This. newMethod1 (); // call the method to implement inheritance
Delete this. newMethod1; // delete the reference of this object to avoid calling errors.

This. newMethod2 = Sex; // object impersonate, pointing to Sex object
This. newMethod2 (sex); // call the method to implement inheritance
Delete this. newMethod2; // delete the reference of this object to avoid calling errors.
}
Var chin1 = new Chinese ("James", "Male ");
Function Human () {// defines the Human class
This. species = "Human ";
}
Function Sex {// defines the sex class
This. sex = sex;
}
Function Chinese (name, sex ){
This. name = name;
This. newMethod1 = Human; // object impersonate, pointing to the Human object
This. newMethod1 (); // call the method to implement inheritance
Delete this. newMethod1; // delete the reference of this object to avoid calling errors.
 
This. newMethod2 = Sex; // object impersonate, pointing to Sex object
This. newMethod2 (sex); // call the method to implement inheritance
Delete this. newMethod2; // delete the reference of this object to avoid calling errors.
}
Var chin1 = new Chinese ("James", "Male ");

 

The object impersonating method is simple and easy to understand. The principle is to use the method call to implement attribute definition in the function.

In addition, object impersonation can implement multi-inheritance, but it is not a good place.

As follows:


[Javascript]

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.