zoukankan      html  css  js  c++  java
  • js面试题--js的继承

      js是门灵活的语言,实现一种功能往往有多种做法,ECMAScript没有明白的继承机制。而是通过模仿实现的。依据js语言的本身的特性,js实现继承有下面通用的几种方式
    1.使用对象冒充实现继承(该种实现方式能够实现多继承)
    实现原理:让父类的构造函数成为子类的方法,然后调用该子类的方法,通过thiskeyword给全部的属性和方法赋值

    Js代码  收藏代码
    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("李");  
    22. mychild.saySomeThing();  

     

    2.採用call方法改变函数上下文实现继承(该种方式不能继承原型链,若想继承原型链,则採用5混合模式)
    实现原理:改变函数内部的函数上下文this,使它指向传入函数的详细对象

    Js代码  收藏代码
    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.   
    13.     this.saySomeThing=function()  
    14.     {  
    15.         console.log(this.fname);  
    16.         this.sayAge();  
    17.     }  
    18.    this.getName=function()  
    19.    {  
    20.        return firstname;  
    21.    }  
    22.   
    23. }  
    24. var child=new Child("张");  
    25. Parent.call(child,child.getName());  
    26. child.saySomeThing();  

     

    3.採用Apply方法改变函数上下文实现继承(该种方式不能继承原型链,若想继承原型链。则採用5混合模式)
    实现原理:改变函数内部的函数上下文this,使它指向传入函数的详细对象

    Js代码  收藏代码
    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.   
    13.     this.saySomeThing=function()  
    14.     {  
    15.         console.log(this.fname);  
    16.         this.sayAge();  
    17.     }  
    18.     this.getName=function()  
    19.     {  
    20.         return firstname;  
    21.     }  
    22.   
    23. }  
    24. var child=new Child("张");  
    25. Parent.apply(child,[child.getName()]);  
    26. child.saySomeThing();  

     

    4.採用原型链的方式实现继承
    实现原理:使子类原型对象指向父类的实例以实现继承,即重写类的原型,弊端是不能直接实现多继承

    Js代码  收藏代码
    1. function Parent()  
    2. {  
    3.   
    4.     this.sayAge=function()  
    5.     {  
    6.         console.log(this.age);  
    7.     }  
    8. }  
    9. function Child(firstname)  
    10. {  
    11.     this.fname=firstname;  
    12.     this.age=40;  
    13.     this.saySomeThing=function()  
    14.     {  
    15.         console.log(this.fname);  
    16.         this.sayAge();  
    17.     }  
    18. }  
    19.   
    20. Child.prototype=new  Parent();  
    21. var child=new Child("张");  
    22. child.saySomeThing();  

     

    5.採用混合模式实现继承

    Js代码  收藏代码
    1. function Parent()  
    2. {  
    3.   
    4.     this.sayAge=function()  
    5.     {  
    6.         console.log(this.age);  
    7.     }  
    8. }  
    9.   
    10. Parent.prototype.sayParent=function()  
    11. {  
    12.    alert("this is parentmethod!!!");  
    13. }  
    14.   
    15. function Child(firstname)  
    16. {  
    17.     Parent.call(this);  
    18.     this.fname=firstname;  
    19.     this.age=40;  
    20.     this.saySomeThing=function()  
    21.     {  
    22.         console.log(this.fname);  
    23.         this.sayAge();  
    24.     }  
    25. }  
    26.   
    27. Child.prototype=new  Parent();  
    28. var child=new Child("张");  
    29. child.saySomeThing();  
    30. child.sayParent();  

     

  • 相关阅读:
    Codeforces Gym 100571A A. Cursed Query 离线
    codeforces Gym 100500 J. Bye Bye Russia
    codeforces Gym 100500H H. ICPC Quest 水题
    codeforces Gym 100500H A. Potion of Immortality 简单DP
    Codeforces Gym 100500F Problem F. Door Lock 二分
    codeforces Gym 100500C D.Hall of Fame 排序
    spring data jpa 创建方法名进行简单查询
    Spring集成JPA提示Not an managed type
    hibernate配置文件中的catalog属性
    SonarLint插件的安装与使用
  • 原文地址:https://www.cnblogs.com/jzssuanfa/p/6899348.html
Copyright © 2011-2022 走看看