js实现继承的5种方式

以下 均为 ES5 的写法:

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();

js实现继承的5种方式 (笔记)的更多相关文章

  1. js 实现继承的几种方式

    //js中实现继承的几种方式 //实现继承首先要有一个父类,先创造一个动物的父类 function Animal(name){ this.name = name; this.shoot = funct ...

  2. js 实现继承的6种方式(逐渐优化)

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  3. js实现继承的两种方式

    这是面试时面试官会经常问到问题: js的继承方式大致可分为两种:对象冒充和原型方式: 一.先说对象冒充,又可分为3种:临时属性方式.call().apply(): 1.临时属性方式: 当构造对象son ...

  4. js实现继承的5种方式

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

  5. 深入浅出js实现继承的7种方式

    给大家介绍7中js继承的方法 有些人认为JavaScript并不是真正的面向对象语言,在经典的面向对象语言中,您可能倾向于定义类对象,然后您可以简单地定义哪些类继承哪些类(参考C++ inherita ...

  6. JS实现继承的几种方式

    前言 JS作为面向对象的弱类型语言,继承也是其非常强大的特性之一.那么如何在JS中实现继承呢?让我们拭目以待. JS继承的实现方式 既然要实现继承,那么首先我们得有一个父类,代码如下: // 定义一个 ...

  7. JavaScript面向对象(三)——继承与闭包、JS实现继承的三种方式

      前  言 JRedu 在之前的两篇博客中,我们详细探讨了JavaScript OOP中的各种知识点(JS OOP基础与JS 中This指向详解 . 成员属性.静态属性.原型属性与JS原型链).今天 ...

  8. JS 面向对象 ~ 继承的7种方式

    前言: 继承 是 OO 语言中的一个最为人津津乐道的概念.许多 OO 语言都支持两种继承方式:接口继承 和 实现继承.接口继承只继承方法签名,而实现继承则继承实际的方法.如前所述,由于函数没有签名,在 ...

  9. JS实现继承的几种方式(转)

    转自:幻天芒的博客 前言 JS作为面向对象的弱类型语言,继承也是其非常强大的特性之一.那么如何在JS中实现继承呢?让我们拭目以待. JS继承的实现方式 既然要实现继承,那么首先我们得有一个父类,代码如 ...

随机推荐

  1. MySQL Performance-Schema(二) 理论篇

    MySQL Performance-Schema中总共包含52个表,主要分为几类:Setup表,Instance表,Wait Event表,Stage Event表Statement Event表,C ...

  2. Oracle学习笔记七 锁

    锁的概念 锁是数据库用来控制共享资源并发访问的机制. 锁用于保护正在被修改的数据 直到提交或回滚了事务之后,其他用户才可以更新数据 对数据的并发控制,保证一致性.完整性.

  3. SVN“验证位置时发生错误”的解决办法

    验证位置时发生错误:“org.tigris.subversion.javahl.ClientException...... 验证位置时发生错误:“org.tigris.subversion.javah ...

  4. ubuntu14.04 yuv文件的播放及视频信息的查看

    1.安装ffmpeg sudo add-apt-repository ppa:mc3man/trusty-media sudo apt-get update sudo apt-get install ...

  5. Linux进程学习

    进程与进程管理: 清屏:system("clear"); //#include <signal.h> 进程环境与进程属性: 什么是进程:简单的说,进程就是程序的一次执行 ...

  6. asp.net缓存

    copy:http://www.cnblogs.com/knowledgesea/archive/2012/06/20/2536603.html 一.缓存概念,缓存的好处.类型.            ...

  7. xml解析

    config.xml <?xml version="1.0" encoding="UTF-8"?> <prize> <gift&g ...

  8. Hibernate 缓存机制浅析

    1. 为什么要用 Hibernate 缓存? Hibernate是一个持久层框架,经常访问物理数据库. 为了降低应用程序对物理数据源访问的频次,从而提高应用程序的运行性能. 缓存内的数据是对物理数据源 ...

  9. 用EmEditor实现PDF转Word后的对齐排版

    Redraw = false//禁止重绘(类似于VBA中的: Application.screenupdating=FALSE),以提高运行效率 //去除所有空行和只由空白字符构成的行 documen ...

  10. from表单提交数组

    页面代码: function submitForm(){ var categoryArray = new Array(); var $ss = $("select[name=industry ...