js是门灵活的语言,实现一种功能往往有多种做法,ECMAScript没有明白的继承机制。而是通过模仿实现的。依据js语言的本身的特性,js实现继承有下面通用的几种方式

1.使用对象冒充实现继承(该种实现方式能够实现多继承)

实现原理:让父类的构造函数成为子类的方法,然后调用该子类的方法,通过thiskeyword给全部的属性和方法赋值

  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,使它指向传入函数的详细对象

  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("张");
  23. Parent.call(child,child.getName());
  24. child.saySomeThing();

3.採用Apply方法改变函数上下文实现继承(该种方式不能继承原型链,若想继承原型链。则採用5混合模式)

实现原理:改变函数内部的函数上下文this,使它指向传入函数的详细对象

  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("张");
  23. Parent.apply(child,[child.getName()]);
  24. child.saySomeThing();

4.採用原型链的方式实现继承

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

  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("张");
  20. child.saySomeThing();

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

  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("张");
  25. child.saySomeThing();
  26. child.sayParent();

js面试题--js的继承的更多相关文章

  1. js面试题--------JS中数字和字符,布尔类型相加相减问题

    JS中数字和字符相加相减问题 <html lang="en"> <head> <meta charset="utf-8" /> ...

  2. js经典试题之原型与继承

    js经典试题之原型与继承 1:以下代码中hasOwnProperty的作用是? var obj={} …….. obj.hasOwnProperty("val") 答案:判断obj ...

  3. vue.js面试题整理

    Vue.js面试题整理 一.什么是MVVM? MVVM是Model-View-ViewModel的缩写.MVVM是一种设计思想.Model 层代表数据模型,也可以在Model中定义数据修改和操作的业务 ...

  4. 2019前端面试系列——JS面试题

    判断 js 类型的方式 1. typeof 可以判断出'string','number','boolean','undefined','symbol' 但判断 typeof(null) 时值为 'ob ...

  5. [ 转载 ] vue.js面试题一

    转载自:https://www.cnblogs.com/aimeeblogs/p/9501490.html 如有侵权 联系删除 Vue.js面试题整理 一.什么是MVVM? MVVM是Model-Vi ...

  6. 2017、2018面试分享(js面试题记录)记得点赞分享哦;让更多的人看到~~

    2017面试分享(js面试题记录) 1. 最简单的一道题 '11' * 2 'a8' * 3 var a = 2, b = 3; var c = a+++b; // c = 5 2. 一道this的问 ...

  7. 10个常见的Node.js面试题

    如果你希望找一份有关Node.js的工作,但又不知道从哪里入手评测自己对Node.js的掌握程度. 本文就为你罗列了10个常见的Node.js面试题,分别考察了Node.js编程相关的几个主要方面. ...

  8. js中的原型、继承的一些想法

    最近看到一个别人写的js类库,突然对js中的原型及继承产生了一些想法,之前也看过其中的一些内容,但是总不是很清晰,这几天利用空闲时间,对这块理解了一下,感觉还是有不通之处,思路上没那么条理,仅作为分享 ...

  9. 所在实习公司的JS笔试题

    在班上无聊的时候看到了一份JS笔试题(我是电面进去的,没做过这份题~~),开始还觉得蛮简单......后来觉得还是很有意思的,贴出来一起看看. 题目一: if(!("a" in w ...

随机推荐

  1. 通过委托事件实现winform窗体之间的互相刷新

    新建winform窗体Form1和Form2; 接下来要通过点击Form2的按钮,实现Form1界面的同步刷新. 先展示一下最终效果: 1.Form1界面如下: 2.点击按钮弹出Form2,界面如下: ...

  2. ORA-03113 ---end-of-file on communication channel 解决方案记录

    ORALCE启动时报如下错误: ORA-03113: end-of-file on communication channel     解决方案如下: 1.查看orcle启动日志,确定具体是什么原因引 ...

  3. asp.net ajax 简单案例

    第一步先引用 scriptManager <asp:UpdatePanel ID="UpdatePanelGuanZhu" runat="server"& ...

  4. Assembly之instruction之CMP

    CMP[.W]  Compare source and destinationCMP.B  Compare source and destination Syntax  CMP src,dst or ...

  5. 3、scala函数入门

    1.定义函数 2.在代码块中定义函数体 3.递归函数与返回类型 4.默认参数 5.带名参数 6.变长参数 7.使用序列调用变长参数  8.过程 9.lazy值              10.异常 1 ...

  6. ASP.NE 上传文件控件

    protected void Button1_Click(object sender, EventArgs e) { //if (Request["id"]==null & ...

  7. Lost connection to MySQL server at 'reading authorization packet', system error: 0_Mysql

    1.大多数时候设置"set global connect_timeout=60:"是可以解决问题的. 我们可以通过执行“SHOWSTATUS LIKE 'aborted%'”,可以 ...

  8. eas之Uuid和BOSUuid 区别

    BOSUuid 加入了BOSType的概念,这个唯一码跟 BOSType有关,里面包含了BOSType的信息. 根据BOSType可以生产BOSUuid,同样,根据BOSUuid也可以找到BOSTyp ...

  9. 数据挖掘系列 (1) 关联规则挖掘基本概念与 Aprior 算法

    转自:http://www.cnblogs.com/fengfenggirl/p/associate_apriori.html 数据挖掘系列 (1) 关联规则挖掘基本概念与 Aprior 算法 我计划 ...

  10. [tyvj1957 Poetize5] Vani和Cl2捉迷藏 (最小路径可重点覆盖+二分图最大匹配)

    传送门 Description 这片树林里有N座房子,M条有向道路,组成了一张有向无环图. 树林里的树非常茂密,足以遮挡视线,但是沿着道路望去,却是视野开阔.如果从房子A沿着路走下去能够到达B,那么在 ...