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]