Home > front end >  Questions about the prototype chain inheritance for superior solutions
Questions about the prototype chain inheritance for superior solutions

Time:09-17

Why this code below, the console output of B type object instance of B, but is considered to be A type? Please answer
 & lt; ! DOCTYPE html> 

Inheritance of four patterns & lt;/title> <br/><meta charset="utf-8" & gt; <br/></head> <br/><body> <br/><The script type="text/javascript" & gt; <br/>Function A () {} <br/>Var a=new a (); <br/>P. rototype=a; <br/>The function B () {} <br/>Var b=new b (); <br/>C.p rototype=b; <br/>The function C () {} <br/>The console. The log (b);//A {} why console output is regarded as A type b? <br/></script> <br/></body> <br/></html> </pre><p class="article - content rp"> CodePudding user response: </p>Because of fn prototype object point to the A B ah! <br/>If you want to make the output of the b is {b}, you can use the following code, modify the prototype of the b, pointing to A prototype, and stipulates the consructor <br/>P. rototype=Object. The create (Amy polumbo rototype, {constructor: {value: A}}); <br/><p class="article - content rp"> CodePudding user response: </p>Thank you, it is so! </div> <div class="th_page th_page_color"></div> <div class="umCopyright"> <p>Page link:<a href="/frontend/7698.html" target="_blank" style="color:#999">https//www.codepudding.com/frontend/7698.html</a></p> </div> <div class="detail-arr"> <div class="detail-arr-left">Prev:<a href='/frontend/7697.html'>Git, who help me cut the branch</a></div> <div class="detail-arr-right">Next:<a href='/frontend/7699.html'>The core asp.net ajax</a></div> </div> </div> </div> </div> </div> <div class="container th_top"> <div class="row"> <div class="col-md-12"> <div class="hot-tags neitags"> <ul> <li><i class="iconfont icon-x-tags"></i> Tags:  </li> <a href='/e/tags/?tagname=JavaScript' target='_blank'>JavaScript</a> </ul> </div> </div> </div> </div> <div class="container th_top"> <div class="row"> <div class="col-md-12"> <div class="xiangguan"> <ul class="msg msghead"> <li class="tbname">Related</li> </ul> <ul> </ul> </div> </div> </div> </div> <div class="container th_top"> <div class="row"> <div class="col-md-12"> <div class="flinks"> <ul> <li><i class="iconfont icon-x-tags"></i> Links:  </li> <li class="liflinks"><a target="_blank" href="/" title="CodePudding">CodePudding</a></li> </ul> </div> </div> </div> </div> <div class="footer"> <p><span style="font-size:16px;color:#666;font-weight: bold">About Us:</span>  <a href="https://www.codepudding.com/contact.html">Contact Us</a>      <a href="https://www.codepudding.com/service.html">Terms of Service</a>      <a href="https://www.codepudding.com/privacy.html"> Privacy Policy</a></p> <p class="foot_info">Copyright © 2010-2023,Powered By <a href="/" target="_blank">CodePudding</a> </p> </div> <script type="text/javascript" src="/skin/code/tianhu.js"></script> </body></html>