JS实现面向对象继承的5种方式分析


Posted in Javascript onJuly 21, 2018

本文实例讲述了JS实现面向对象继承的5种方式。分享给大家供大家参考,具体如下:

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

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

实现原理:让父类的构造函数成为子类的方法,然后调用该子类的方法,通过this关键字给所有的属性和方法赋值

function Parent(firstname)
{
  this.fname=firstname;
  this.age=40;
  this.sayAge=function()
  {
    console.log(this.age);
  }
}
function Child(firstname)
{
  this.parent=Parent;
  this.parent(firstname);
  delete this.parent;
  this.saySomeThing=function()
  {
    console.log(this.fname);
    this.sayAge();
  }
}
var mychild=new Child("李");
mychild.saySomeThing();

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

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

function Parent(firstname)
{
  this.fname=firstname;
  this.age=40;
  this.sayAge=function()
  {
    console.log(this.age);
  }
}
function Child(firstname)
{
  this.saySomeThing=function()
  {
    console.log(this.fname);
    this.sayAge();
  }
  this.getName=function()
  {
    return firstname;
  }
}
var child=new Child("张");
Parent.call(child,child.getName());
child.saySomeThing();

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

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

function Parent(firstname)
{
  this.fname=firstname;
  this.age=40;
  this.sayAge=function()
  {
    console.log(this.age);
  }
}
function Child(firstname)
{
  this.saySomeThing=function()
  {
    console.log(this.fname);
    this.sayAge();
  }
  this.getName=function()
  {
    return firstname;
  }
}
var child=new Child("张");
Parent.apply(child,[child.getName()]);
child.saySomeThing();

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

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

function Parent()
{
  this.sayAge=function()
  {
    console.log(this.age);
  }
}
function Child(firstname)
{
  this.fname=firstname;
  this.age=40;
  this.saySomeThing=function()
  {
    console.log(this.fname);
    this.sayAge();
  }
}
Child.prototype=new Parent();
var child=new Child("张");
child.saySomeThing();

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

function Parent()
{
  this.sayAge=function()
  {
    console.log(this.age);
  }
}
Parent.prototype.sayParent=function()
{
  alert("this is parentmethod!!!");
}
function Child(firstname)
{
  Parent.call(this);
  this.fname=firstname;
  this.age=40;
  this.saySomeThing=function()
  {
    console.log(this.fname);
    this.sayAge();
  }
}
Child.prototype=new Parent();
var child=new Child("张");
child.saySomeThing();
child.sayParent();

希望本文所述对大家JavaScript程序设计有所帮助。

Javascript 相关文章推荐
一段非常简单的js判断浏览器的内核
Aug 17 Javascript
javascript如何定义对象数组
Jun 07 Javascript
javascript iframe跨域详解
Oct 26 Javascript
js获取指定时间的前几秒
Apr 05 Javascript
angular中不同的组件间传值与通信的方法
Nov 04 Javascript
浅谈 vue 中的 watcher
Dec 04 Javascript
JavaScript如何对图片进行黑白化
Apr 10 Javascript
angular6.0开发教程之如何安装angular6.0框架
Jun 29 Javascript
vue-cli脚手架搭建的项目去除eslint验证的方法
Sep 29 Javascript
通过JQuery,JQueryUI和Jsplumb实现拖拽模块
Jun 18 jQuery
vue中beforeRouteLeave实现页面回退不刷新的示例代码
Nov 01 Javascript
解决ant design vue 表格a-table二次封装,slots渲染的问题
Oct 28 Javascript
JavaScript数组基于交换的排序示例【冒泡排序】
Jul 21 #Javascript
vue项目中添加单元测试的方法
Jul 21 #Javascript
基于Vue实现关键词实时搜索高亮显示关键词
Jul 21 #Javascript
jQuery实现的点击按钮改变样式功能示例
Jul 21 #jQuery
jQuery实现输入框的放大和缩小功能示例
Jul 21 #jQuery
jQuery实现table表格信息的展开和缩小功能示例
Jul 21 #jQuery
浅谈vue父子组件怎么传值
Jul 21 #Javascript
You might like
PHP中的str_repeat函数在JavaScript中的实现
2013/09/16 PHP
php使用$_POST或$_SESSION[]向js函数传参
2014/09/16 PHP
php+redis实现多台服务器内网存储session并读取示例
2017/01/12 PHP
php实现通过soap调用.Net的WebService asmx文件
2017/02/27 PHP
Yii框架创建cronjob定时任务的方法分析
2017/05/23 PHP
php学习笔记之mb_strstr的基本使用
2018/02/03 PHP
Laravel推荐使用的十个辅助函数
2019/05/10 PHP
laravel实现前后台路由分离的方法
2019/10/13 PHP
js里的prototype使用示例
2010/11/19 Javascript
jQuery 常见操作实现方式和常用函数方法总结
2011/05/06 Javascript
网站内容禁止复制和粘贴、另存为的js代码
2014/02/26 Javascript
运行Node.js的IIS扩展iisnode安装配置笔记
2015/03/02 Javascript
Javascript类型系统之String字符串类型详解
2016/06/21 Javascript
jquery 抽奖小程序实现代码
2016/10/12 Javascript
jQuery判断邮箱格式对错实例代码讲解
2017/04/12 jQuery
带你了解session和cookie作用原理区别和用法
2017/08/14 Javascript
深入理解JS的事件绑定、事件流模型
2018/05/13 Javascript
jquery.pager.js分页实现详解
2019/07/29 jQuery
使用Angular material主题定义自己的组件库的配色体系
2019/09/04 Javascript
javaScript中indexOf用法技巧
2019/11/26 Javascript
[07:49]2014DOTA2国际邀请赛 Newbee夺冠后采访xiao8坦言奖金会上交
2014/07/23 DOTA
[02:26]2016国际邀请赛8月3日开战 中国军团出征西雅图
2016/08/02 DOTA
用实例分析Python中method的参数传递过程
2015/04/02 Python
Python的Flask站点中集成xhEditor文本编辑器的教程
2016/06/13 Python
pycharm 批量修改变量名称的方法
2019/08/01 Python
Python编写打字训练小程序
2019/09/26 Python
django ListView的使用 ListView中获取url中的参数值方式
2020/03/27 Python
Django admin管理工具TabularInline类用法详解
2020/05/14 Python
linux比较文件内容的命令是什么
2013/03/04 面试题
教师师德教育的自我评价
2013/10/31 职场文书
幼儿园教育教学反思
2014/01/31 职场文书
表演方阵解说词
2014/02/08 职场文书
创先争优标语
2014/06/27 职场文书
Python代码,能玩30多款童年游戏!这些有几个是你玩过的
2021/04/27 Python
浅谈由position属性引申的css进阶讨论
2021/05/25 HTML / CSS
python ansible自动化运维工具执行流程
2021/06/24 Python