javascript 继承实现方法
时间:2024-04-19 11:04:12
下面我给出几种常用的方法:
1 .对象冒充
原理: 构造函数使用this关键字给所有属性和方法赋值, 因为构造函数只是一个函数,所以可以使ClassA的构造函数成为classB的方法,然后调用它.这样classB就会收到classA的构造函数中定义的属性和方法.例子:
function classA(name)
{
this.name=name;
this.showName=function(){alert(this.name);}
}
function classB(name)
{
this.newMethod = classA;
this.newMethod(name);
}
obj = new classA("hero");
objB = new classB("dby");
obj.showName(); // print hero
objB.showName(); // print dby 说明classB 继承了classA的方法.
对象冒充可以实现多重继承 例如
function classz(){
this.newMethod = classX;
this.newMethod();
delete this.newMethod;
this.newMethod=classY;
this.newMethod():
delete this.newMethod;
}
但是如果classX和classY有相同的属性或者方法,classY具有高优先级.
2.call()方法
call方法使与经典的对象冒充法就相近的方法,它的第一个参数用作this的对象,其他参数都直接传递给函数自身.
function sayName(perfix)
{
alert(perfix+this.name);
}
obj= new Object();
obj.name="hero";
sayName.call(obj,"hello," );
function classA(name)
{
this.name=name;
this.showName=function(){alert(this.name);};
}
function classB(name)
{
classA.call(this,name);
}
objB = new classB("bing");
objB.showName();////说明classB继承classA的showName方法
3.apply()方法
aplly()方法有2个参数,一个用作this对象,一个使传递给函数的参数数组.
function sayName(perfix)
{
alert(perfix+this.name);
}
obj= new Object();
obj.name="hero";
sayName.aplly(obj,new Array("hello,") );
4. 原型链
prototype对象的任何属性和方法都会被传递给对应类的所有实例,原型链就是用这种方式来显现继承.
function classA (){}
classA.prototype.name="hero";
classA.prototype.showName=function(){alert(this.name)}
function classB(){}
classB.prototype=new classA();
objb = new classB()
objb.showName();//print hero 说明b继承了a的方法
这里需要注意 调用classA的构造函数时,没有给它传递参数,这是原型链的标准做法,确保函数的构造函数没有任何参数.
并且 子类的所有属性和方法,必须出现在prototype属性被赋值后,应为在它之前赋的值会被删除.因为对象的prototype属性被替换成了新对象,添加了新方法的原始对象将被销毁.
5 混和方式
就是用冒充方式 定义构造函数属性,用原型法定义对象方法.
function classA(name)
{
this.name=name;
}
classA.prototype.showName=function(){alert(this.name)}
function classB(name)
{
classA.call(this,name);
}
classB.prototype = new classA();
classB.prototype.showName1=function(){alert(this.name+"*****");};
obj = new classB("hero");
obj.showName();
obj.showName1();
在classB的构造函数中通过调用call方法 继承classA中的name属性,用原型链来继承classA的showName方法.
标签:javascript,继承
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python 制作手机归属地查询工具(附源码)
2023-07-05 07:02:34
![](https://img.aspxhome.com/file/2023/2/118072_0s.png)
Vue如何引入远程JS文件
2023-07-02 16:34:13
JS获取当前时间的年月日时分秒及时间的格式化的方法
2024-04-17 10:23:00
vue-router路由懒加载和权限控制详解
2024-04-27 15:51:24
SQL Server的触发器详解
2024-01-21 16:26:58
基于Php mysql存储过程的详解
2024-06-05 09:22:00
python实现图像识别功能
2023-08-09 08:45:00
python调用摄像头显示图像的实例
2021-07-18 20:46:58
python FTP批量下载/删除/上传实例
2022-11-21 15:27:59
![](https://img.aspxhome.com/file/2023/9/105769_0s.jpg)
MySQL中几种插入和批量语句实例详解
2024-01-26 22:09:06
Django Admin中增加导出CSV功能过程解析
2021-04-17 06:22:47
![](https://img.aspxhome.com/file/2023/1/115781_0s.jpg)
python 实现弹球游戏的示例代码
2022-04-08 20:06:42
![](https://img.aspxhome.com/file/2023/9/120299_0s.gif)
javascript、php关键字搜索函数的使用方法
2024-05-08 10:10:19
大写锁定提醒
2009-06-11 13:12:00
![](https://img.aspxhome.com/file/UploadPic/20096/11/win2003_capslk-48s.png)
python 下 CMake 安装配置 OPENCV 4.1.1的方法
2022-04-04 07:45:21
![](https://img.aspxhome.com/file/2023/5/100275_0s.png)
getElementsByAttribute
2009-10-27 12:13:00
Python使用Scrapy爬取妹子图
2022-06-17 23:47:41
Python边缘检测之prewitt,sobel和laplace算子详解
2023-09-30 06:01:56
![](https://img.aspxhome.com/file/2023/0/135090_0s.png)
Python子进程subpocess原理及用法解析
2021-10-11 19:18:27
ExtJS判断IE浏览器类型的方法
2024-05-13 10:35:56