JS Wrapper inheritance function

Source: Internet
Author: User
Tags hasownproperty

1 functionExtend (child,parent) {2     varf=function(){}3F.prototype=Parent.prototype;4Child.prototype=NewF ();5Child.prototype=Child ;6Child.uber=Parent.prototype;//uber a prototype that points to the parent object. Facilitates the child object to invoke the overridden method in the parent object prototype. 7 }8 functionShape () {}9Shape.prototype.name= "Shape";TenShape.prototype.color= "Blue"; One  A functionCircle (RADIUS) { -      This. radius=radius; - } the  -Circle.prototype.name= "Circle"; -circle.prototype.gets=function(){ -     return  This. radius* This. radius*3.14; + } - extend (circle,shape); +  A varCir=NewCircle (2); atalert (cir.name);//"Circle" -alert (Cir.color);//"Blue" -alert (cir.uber.name);//"Shape" -Alert (Cir.gets ())//"12.56"


The first parameter of the Extend function is the sub-constructor, and the second argument is the parent constructor;

Why the prototype object of a sub-constructor does not directly point to the parent constructor's prototype object, because if Child.prototype=parent.prototype, then the parent-Child prototype object is bound together, that is, the parent-child's prototype object is equal to the reference, which is equivalent to pointing to the same address, The prototype of the child object is changed, and the prototype of the parent object is changed, and if the prototype of the parent object and the child object are traversed, two are found to be identical.

1 varCir=NewCircle (2);2 varshape1=NewShape ();3Cir.prototype.name= "This is Circle";4 5 6  for(varIinchshape1) {7      if(!shape1.hasownproperty (i)) {//traversing non-instance properties8Console.log (i+ ":" +shape1[i]);9      }Ten } One  A //name:circle - //Color:blue - //gets:function () { the     return  This. radius* This. radius*3.14; - } -  -  +  for(varIinchCir) { -      if(!Cir.hasownproperty (i)) { +Console.log (i+ ":" +cir[i]); A      } at } -  - //name:circle - //Color:blue - //gets:function () { -     return  This. radius* This. radius*3.14; in}.

JS Wrapper inheritance function

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.