函数继承

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>05-继承的概念</title>
<script>
/*
function Dog (color,name){
this.skinColor = color;
this.name = name;
this.act = function (){
console.log(this.name + '汪汪汪');
}
}

function Person (color,name){
this.skinColor = color;
this.name = name;
this.act = function(){
console.log(this.name + '去遛狗');
}
}

var erha = new Dog('白色','二哈');
var xiaopao = new Person('黄色','小炮');
*/
// 重构上面的构造函数
// 定义一个父级构造函数
function Biology(color,name,fun){
this.skinColor = color;
this.name = name;
this.act = fun;
}

// 下面两个构造函数通过apply和call方法来继承上面构造函数Biology的属性
function Dog(){
// 使用apply或者call方法调用Biology函数
Biology.apply(this,arguments);
}
var erha = new Dog('白色','二哈',function(){console.log(this.name + '汪汪汪');});


function Person(){
// 使用apply或者call方法调用Biology函数
Biology.call(this,arguments[0],arguments[1],arguments[2]);
this.job = arguments[3];
}
var xiaopao = new Person('黄色','小炮',function(){console.log(this.name + '散步');},'coder');

xiaopao.act();

// 对象的嵌套
xiaopao.pet = erha;
// 覆盖act属性
xiaopao.act = function(){
console.log(this.name + '和他的宠物' + this.pet.name + '一起去散步');
};
xiaopao.act();

 

// 对象嵌套,字面量
var erha2 = {
name:'二哈',
act:function(){
console.log(this.name + '汪汪汪');
}
}

var xiaohua = {
name:'小花',
pet:erha2,
act:function(){
console.log(this.name + '和他的宠物' + this.pet.name + '一起去散步');
}
}
xiaohua.act();

 

 

</script>
</head>
<body>

</body>
</html>

转载于:https://www.cnblogs.com/qh926/p/6087873.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值