If you are used to understanding and analyzing problems from the perspective of "class", then the inheritance implementation based on constructors is more suitable for you, and the 01~06 method is based on constructors.
If you are only dealing with specific objects or instances, then the object-based inheritance implementation is more appropriate for you, and the 07~12 approach is object-based.
| number |
prototype chain |
example |
| 01 |
prototype chain (Classic mode) |
Child.prototype = new parent (); |
| 02 |
Inherit only the prototype of the parent constructor |
Child. prototype = Parent. prototype; |
| 03 |
Borrowing constructors |
function Child () { parent.apply (thisarguments);} |
| 04 |
Temporary constructors |
function extend(child,parent) { varF = function() {}; F.prototype =Parent. prototype; Child.prototype =NewF (); Child.prototype.constructor = child; Child.uber =Parent. prototype;} |
| 05 |
Copy the prototype property of the parent constructor |
function extend2 (Child, Parent) { var p = Parent.prototype; var C = child.prototype; for (Vari in P) {C[i] = P[i]; } c.uber = P;} |
| 06 |
Borrowing constructors and copying prototypes |
function Child () { parent.apply (thisarguments);} Extend2 (child,parent); |
| 07 |
object-based shallow copy |
function shallowcopy (p) { var c = {}; for (var i in p) {C[i] = P[i]; } c.uber = P; return C;} |
| 08 |
Deep copy of Object-based |
function shallowcopy (p) { var c = {}; for (var i in p) {C[i] = P[i]; } c.uber = P; return C;} |
| 09 |
Prototype inheritance |
function object (o) { function f () {} f.prototype = O; return new F ();}
|
| 10 |
Hybrid mode of prototype inheritance and attribute copy |
function objectplus(o, Stuff) { varN function F() {} f.prototype = O; n =NewF (); N.uber = O; for(varIinchStuff) {N[i] = Stuff[i]; }returnn;} |
| 11 |
Multiple inheritance |
function multi() { varn = {},stuff, j =0, Len =arguments. length; for(j =0; J < Len; J + +) {stuff =arguments[j]; for(varIinchStuff) {N[i] = Stuff[i]; } }returnn;} |
| 12 |
Parasitic inheritance |
function parasite (victim) { var that = object (victim); 1; return that;} |
Parameter link: http://www.cnblogs.com/keepfool/p/5592256.html
12 types of routines that JavaScript implements to inherit