js实现继承的5种方式


Posted in Javascript onDecember 01, 2015

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

1、继承第一种方式:对象冒充

function Parent(username){
  this.username = username;
  this.hello = function(){
   alert(this.username);
  }
}
function Child(username,password){
  //通过以下3行实现将Parent的属性和方法追加到Child中,从而实现继承
  //第一步:this.method是作为一个临时的属性,并且指向Parent所指向的对象,
  //第二步:执行this.method方法,即执行Parent所指向的对象函数
  //第三步:销毁this.method属性,即此时Child就已经拥有了Parent的所有属性和方法 
  this.method = Parent;
  this.method(username);//最关键的一行
  delete this.method;
  this.password = password;
  this.world = function(){
   alert(this.password);
  }
}
var parent = new Parent("zhangsan");
var child = new Child("lisi","123456");
parent.hello();
child.hello();
child.world();

2、继承第二种方式:call()方法方式

call方法是Function类中的方法
call方法的第一个参数的值赋值给类(即方法)中出现的this
call方法的第二个参数开始依次赋值给类(即方法)所接受的参数

function test(str){
  alert(this.name + " " + str);
}
var object = new Object();
object.name = "zhangsan";
test.call(object,"langsin");//此时,第一个参数值object传递给了test类(即方法)中出现的this,而第二个参数"langsin"则赋值给了test类(即方法)的str
function Parent(username){
  this.username = username;
  this.hello = function(){
   alert(this.username);
  }
}
function Child(username,password){
  Parent.call(this,username);
  this.password = password;
  this.world = function(){
   alert(this.password);
  }
}
var parent = new Parent("zhangsan");
var child = new Child("lisi","123456");
parent.hello();
child.hello();
child.world();

3、继承的第三种方式:apply()方法方式

apply方法接受2个参数,
A、第一个参数与call方法的第一个参数一样,即赋值给类(即方法)中出现的this
B、第二个参数为数组类型,这个数组中的每个元素依次赋值给类(即方法)所接受的参数

function Parent(username){ 
  this.username = username; 
  this.hello = function(){ 
   alert(this.username); 
  } 
} 
function Child(username,password){ 
  Parent.apply(this,new Array(username)); 
  this.password = password; 
  this.world = function(){ 
   alert(this.password); 
  } 
} 
var parent = new Parent("zhangsan"); 
var child = new Child("lisi","123456"); 
parent.hello(); 
child.hello(); 
child.world();

4、继承的第四种方式:原型链方式,即子类通过prototype将所有在父类中通过prototype追加的属性和方法都追加到Child,从而实现了继承

function Person(){ 
} 
Person.prototype.hello = "hello"; 
Person.prototype.sayHello = function(){ 
  alert(this.hello); 
} 
function Child(){ 
} 
Child.prototype = new Person();//这行的作用是:将Parent中将所有通过prototype追加的属性和方法都追加到Child,从而实现了继承 
Child.prototype.world = "world"; 
Child.prototype.sayWorld = function(){ 
  alert(this.world); 
} 
var c = new Child(); 
c.sayHello(); 
c.sayWorld();

5、继承的第五种方式:混合方式

混合了call方式、原型链方式

function Parent(hello){ 
  this.hello = hello; 
} 
Parent.prototype.sayHello = function(){ 
  alert(this.hello); 
} 
function Child(hello,world){ 
  Parent.call(this,hello);//将父类的属性继承过来 
  this.world = world;//新增一些属性 
} 
Child.prototype = new Parent();//将父类的方法继承过来 
Child.prototype.sayWorld = function(){//新增一些方法 
  alert(this.world); 
} 
var c = new Child("zhangsan","lisi"); 
c.sayHello(); 
c.sayWorld();

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

Javascript 相关文章推荐
Extjs列表详细信息窗口新建后自动加载解决方法
Apr 02 Javascript
JavaScript判断访问的来源是手机还是电脑,用的哪种浏览器
Dec 12 Javascript
如何让你的Lightbox支持滚轮缩放及Base64图片
Dec 04 Javascript
jQuery拖动布局其结果保存到数据库
Oct 09 Javascript
jQuery-1.9.1源码分析系列(十)事件系统之事件包装
Nov 20 Javascript
angular+bootstrap的双向数据绑定实例
Mar 03 Javascript
10个经典的网页鼠标特效代码
Jan 09 Javascript
Vue中v-for的数据分组实例
Mar 07 Javascript
解决vue-cli创建项目的loader问题
Mar 13 Javascript
bootstrap中selectpicker下拉框使用方法实例
Mar 22 Javascript
vue中js判断长时间不操作界面自动退出登录(推荐)
Jan 22 Javascript
JavaScript实现沿五角星形线摆动的小圆实例详解
Jul 28 Javascript
6种javascript显示当前系统时间代码
Dec 01 #Javascript
基于jQuery实现网页打印功能
Dec 01 #Javascript
jQuery-1.9.1源码分析系列(十一)DOM操作续之克隆节点
Dec 01 #Javascript
快速学习jQuery插件 Cookie插件使用方法
Dec 01 #Javascript
快速学习jQuery插件 jquery.validate.js表单验证插件使用方法
Dec 01 #Javascript
JavaScript使用DeviceOne开发实战(二) 生成调试安装包
Dec 01 #Javascript
JavaScript使用DeviceOne开发实战(一) 配置和起步
Dec 01 #Javascript
You might like
javascript 定义初始化数组函数
2009/09/07 Javascript
基于jquery的从一个页面跳转到另一个页面的指定位置的实现代码(带平滑移动的效果)
2011/05/24 Javascript
js 通用javascript函数库整理
2011/08/14 Javascript
jquery选择器的选择使用及性能介绍
2013/01/16 Javascript
文字不间断滚动(上下左右)实例代码
2013/04/21 Javascript
JAVASCRIPT函数作用域和提前声明 分享
2013/08/22 Javascript
9行javascript代码获取QQ群成员具体实现
2013/10/16 Javascript
js数组操作学习总结
2013/11/04 Javascript
js判断undefined类型,undefined,null, 的区别详细解析
2013/12/16 Javascript
js购物车实现思路及代码(个人感觉不错)
2013/12/23 Javascript
javascript中拼接HTML字符串的最快、最好的方法
2014/06/07 Javascript
JavaScript获取当前网页标题(title)的方法
2015/04/03 Javascript
Javascript编写俄罗斯方块思路及实例
2015/07/07 Javascript
基于jQuery实现发送短信验证码后的倒计时功能(无视页面关闭)
2016/09/02 Javascript
jQuery中的AjaxSubmit使用讲解
2016/09/25 Javascript
原生JS实现图片轮播切换效果
2016/12/15 Javascript
一句jQuery代码实现返回顶部效果(简单实用)
2016/12/28 Javascript
Vue中v-show添加表达式的问题(判断是否显示)
2018/03/26 Javascript
详解js中的几种常用设计模式
2020/07/16 Javascript
js实现简易计算器小功能
2020/11/18 Javascript
Vue+scss白天和夜间模式切换功能的实现方法
2021/01/05 Vue.js
[26:21]浴火之凤-TI4世界冠军Newbee战队纪录片
2014/08/07 DOTA
[00:15]天涯墨客终极技能展示
2018/08/25 DOTA
python使用in操作符时元组和数组的区别分析
2015/05/19 Python
python 实现selenium断言和验证的方法
2019/02/13 Python
django 取消csrf限制的实例
2020/03/13 Python
Python爬虫爬取ts碎片视频+验证码登录功能
2021/02/22 Python
移动端rem布局的两种实现方法
2018/01/03 HTML / CSS
新西兰床上用品和家居用品购物网站:Adairs
2018/04/27 全球购物
英国银首饰公司:e&e Jewellery
2021/02/11 全球购物
金智子午JAVA面试题
2015/09/04 面试题
甜品蛋糕店创业计划书范文
2014/02/06 职场文书
遗嘱格式范本
2015/08/07 职场文书
小学英语听课心得体会
2016/01/14 职场文书
党员理论学习心得体会
2016/01/21 职场文书
穷人该怎么创业?谨记以下几点
2019/07/11 职场文书