003 js的类 ,实例 与 继承

 js的类 ,实例 与 继承 

function Enemy(name,level){
this.name=name;
this.level=level;
}

Enemy.prototype.attack_player=function(){
console.log("attack player!");
}


///=====Enemy.prototype 类原型=====


module.exports=Enemy;

// 继承机制

//***********************
function BossEnemy(name,level){
Enemy.call(this,name,level);
this.blood=100;
}
//写法一
BossEnemy.prototype={};
for(var i in Enemy.prototype){
BossEnemy.prototype[i]=Enemy.prototype[i];
}
//写法二
var a=function{};
a.prototype=Enemy.prototype;
BossEnemy.prototype=new a();

//
BossEnemy.prototype.boss_attack=function(){
console.log("boss attack!");

}

var boss=new BossEnemy("通天教主",99);
boss.boss_attack();
boss.attack_player();


BossEnemy.prototype.attack_player=function(){
//重载
Enemy.prototype.attack_player.call(this); 
console.log("BossEnemy get name!");
return this.name; 
}

boss.attack_player();

//写一个继承函数

 

转载于:https://www.cnblogs.com/iflii/p/10191173.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值