I learned the inheritance of js and wrote it myself. Not very good. Nowadays, many codes are encapsulated into classes. Base. js -- Inherited implementation ==========================
[Note]: After inheritance, if the parent class is a class, it will inherit its attributes, methods (including declared using prototype), and static methods. Otherwise, only attributes and methods are available.
The Code is as follows:
Object. prototype. extendf = function (a, B ){
If (! A |! B) return;
Var fa = typeof a = "function ";
Var fb = typeof B = "function ";
Var cha = function (a, B ){
For (var c in B ){
If (a [c] = undefined) // rewrite the subclass
A [c] = B [c];
}
Return a; // return the inherited object
}
If (fa & fb ){
B. apply (this, a. arguments );
Cha (a, B );
This ["base"] = new B; // access the parent class through base
Return cha (this, B. prototype );
}
Else if (! Fa & fb ){
Cha (a, new B );
A ["base"] = new B;
Return cha (a, B );
} Else if (fa &&! Fb ){
Cha (a, B );
This ["base"] = B;
Return cha (this, B );
} Else if (! Fa &&! Fb ){
A ["base"] = B;
Return cha (a, B );
}
}
Test page: Usage
The Code is as follows: