12 types of routines that JavaScript implements to inherit

Source: Internet
Author: User

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

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.