javascrip关于继承的小例子
时间:2024-04-22 22:17:49
//继承
function Person(name,sex)
{
this.name=name;
this.sex=sex;
}
Person.prototype.sayName=function()
{
alert(this.name);
}
Person.prototype.saySex=function()
{
alert(this.sex);
}
function Worker(name,sex,job)
{
//继承person类
Person.call(this,name,sex) //这里的this指的是Worker类的实例,如下面的'W' ,把W传入Person构造函数,这时W伪装成Person构造函数里的this
this.job=job;
}
//Worker.prototype=Person.prototype;//如果这样负值原型,子类的sayJob方法Person父类也会有sayJob方法,因为是引用传递
//改成如下方式则子类不会影响父类:
for(var i in Person.prototype)
{
Worker.prototype[i]=Person.prototype[i];
}
Worker.prototype.sayJob=function()
{
alert(this.job);
}
var p=new Person('lisi','男');
//alert(p.sayJob);
var w=new Worker('zhangsan','男','打酱油的');
w.sayName();
w.saySex();
w.sayJob();
标签:javascrip,继承
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python实现图像增强
2022-07-08 10:50:50
![](https://img.aspxhome.com/file/2023/9/80549_0s.jpg)
Pytest+Request+Allure+Jenkins实现接口自动化
2021-04-09 13:50:44
![](https://img.aspxhome.com/file/2023/3/112593_0s.png)
pytorch 计算ConvTranspose1d输出特征大小方式
2021-08-14 10:16:16
![](https://img.aspxhome.com/file/2023/3/118083_0s.jpg)
移动网站开发:标记语言
2010-06-08 13:42:00
![](https://img.aspxhome.com/file/UploadPic/20106/8/0pqh1z27_lit-15s.jpg)
python flask搭建web应用教程
2023-05-14 14:16:17
![](https://img.aspxhome.com/file/2023/1/128491_0s.jpg)
golang 执行命令行的实现
2024-04-28 09:14:40
python识别围棋定位棋盘位置
2023-01-09 01:49:34
![](https://img.aspxhome.com/file/2023/9/90799_0s.jpg)
在Infopath中实现数据有效性验证的三种方法
2023-03-06 15:25:31
![](https://img.aspxhome.com/file/2023/0/132290_0s.png)
利用MySQL空间函数实现位置打卡的完整步骤
2024-01-28 11:23:32
![](https://img.aspxhome.com/file/2023/2/67582_0s.png)
ASP 使用jqGrid实现读写删的代码(json)
2010-03-07 17:26:00
python一行输入n个数据问题
2023-09-11 21:50:48
![](https://img.aspxhome.com/file/2023/5/78715_0s.png)
python-pandas创建Series数据类型的操作
2022-01-29 14:16:24
![](https://img.aspxhome.com/file/2023/5/82805_0s.png)
Python检查图片是否损坏及图片类型是否正确过程详解
2021-04-07 22:26:12
![](https://img.aspxhome.com/file/2023/2/87992_0s.png)
Python实现的简单hangman游戏实例
2021-04-11 19:26:47
一个简单的python爬虫程序 爬取豆瓣热度Top100以内的电影信息
2023-01-09 19:50:16
![](https://img.aspxhome.com/file/2023/3/79623_0s.jpg)
python处理圆角图片、圆形图片的例子
2021-06-08 15:20:15
![](https://img.aspxhome.com/file/2023/6/92356_0s.png)
Python实现的HMacMD5加密算法示例
2022-01-15 06:05:31
ASP中的全角和半角之间的转化
2008-06-06 13:34:00
![](https://img.aspxhome.com/file/UploadPic/20086/6/dbc2sbc_35s.gif)
Goland使用Go Modules创建/管理项目的操作
2024-04-30 10:03:02
![](https://img.aspxhome.com/file/2023/0/130730_0s.png)
Go素数筛选分析详解
2023-07-22 11:50:02
![](https://img.aspxhome.com/file/2023/3/90493_0s.png)