Js interview questions-inheritance of js, js questions-js

Source: Internet
Author: User

Js interview questions-inheritance of js, js questions-js

Js is a flexible language. There are often multiple ways to implement a function. ECMAScript does not have a clear Inheritance Mechanism, but is achieved through imitation. According to the characteristics of js, js implementation inheritance has the following common methods:
1. Use object impersonating to implement inheritance (this implementation method can implement multiple inheritance)
Implementation principle: Let the constructor of the parent class become the sub-class method, then call the sub-class method, assign values to all attributes and methods through the this keyword

Js Code
  1. Function Parent (firstname)
  2. {
  3. This. fname = firstname;
  4. This. age = 40;
  5. This. sayAge = function ()
  6. {
  7. Console. log (this. age );
  8. }
  9. }
  10. Function Child (firstname)
  11. {
  12. This. parent = Parent;
  13. This. parent (firstname );
  14. Delete this. parent;
  15. This. saySomeThing = function ()
  16. {
  17. Console. log (this. fname );
  18. This. sayAge ();
  19. }
  20. }
  21. Var mychild = new Child ("Li ");
  22. Mychild. saySomeThing ();

 

2. Use the call method to change the context of the function to implement inheritance (this method cannot inherit the prototype chain. If you want to inherit the prototype chain, use the 5 hybrid mode)
Implementation principle: change the context of the function. this directs it to the specific object of the input function.

Js Code
  1. Function Parent (firstname)
  2. {
  3. This. fname = firstname;
  4. This. age = 40;
  5. This. sayAge = function ()
  6. {
  7. Console. log (this. age );
  8. }
  9. }
  10. Function Child (firstname)
  11. {
  12. This. saySomeThing = function ()
  13. {
  14. Console. log (this. fname );
  15. This. sayAge ();
  16. }
  17. This. getName = function ()
  18. {
  19. Return firstname;
  20. }
  21. }
  22. Var child = new Child ("Zhang ");
  23. Parent. call (child, child. getName ());
  24. Child. saySomeThing ();

 

3. Use the Apply method to change the context of the function to implement inheritance (this method cannot inherit the prototype chain. If you want to inherit the prototype chain, use the 5 hybrid mode)
Implementation principle: change the context of the function. this directs it to the specific object of the input function.

Js Code
  1. Function Parent (firstname)
  2. {
  3. This. fname = firstname;
  4. This. age = 40;
  5. This. sayAge = function ()
  6. {
  7. Console. log (this. age );
  8. }
  9. }
  10. Function Child (firstname)
  11. {
  12. This. saySomeThing = function ()
  13. {
  14. Console. log (this. fname );
  15. This. sayAge ();
  16. }
  17. This. getName = function ()
  18. {
  19. Return firstname;
  20. }
  21. }
  22. Var child = new Child ("Zhang ");
  23. Parent. apply (child, [child. getName ()]);
  24. Child. saySomeThing ();

 

4. Implement inheritance using prototype chain
Implementation principle: enables the subclass prototype object to point to the parent class instance for inheritance, that is, the prototype of the override class. The disadvantage is that multiple inheritance cannot be implemented directly.

Js Code
  1. Function Parent ()
  2. {
  3. This. sayAge = function ()
  4. {
  5. Console. log (this. age );
  6. }
  7. }
  8. Function Child (firstname)
  9. {
  10. This. fname = firstname;
  11. This. age = 40;
  12. This. saySomeThing = function ()
  13. {
  14. Console. log (this. fname );
  15. This. sayAge ();
  16. }
  17. }
  18. Child. prototype = new Parent ();
  19. Var child = new Child ("Zhang ");
  20. Child. saySomeThing ();

 

5. Implement inheritance in hybrid mode

Js Code
  1. Function Parent ()
  2. {
  3. This. sayAge = function ()
  4. {
  5. Console. log (this. age );
  6. }
  7. }
  8. Parent. prototype. sayParent = function ()
  9. {
  10. Alert ("this is parentmethod !!! ");
  11. }
  12. Function Child (firstname)
  13. {
  14. Parent. call (this );
  15. This. fname = firstname;
  16. This. age = 40;
  17. This. saySomeThing = function ()
  18. {
  19. Console. log (this. fname );
  20. This. sayAge ();
  21. }
  22. }
  23. Child. prototype = new Parent ();
  24. Var child = new Child ("Zhang ");
  25. Child. saySomeThing ();
  26. Child. sayParent ();

 

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.