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();

//写一个继承函数

猜你喜欢

转载自www.cnblogs.com/iflii/p/10191173.html