十一 JS继承

// time:2016.2.1
// des:继承
function Enemy() { 
    this.level = 50;  
    console.log("Enemy constructor"); 
}

Enemy.prototype.attack_play = function(){
    console.log("attack_play");
};
Enemy.prototype.wudiai = 100;
Enemy.wudiai = "1213";
Enemy.gongji = function(){
    console.log("gongji   asasasd "+ Enemy.wudiai);
}
function BossEnemy(){
    Enemy.call(this);
    console.log("Boss constructor");
}

// 写法1
// BossEnemy.prototype = {constructor: BossEnemy,};
// for(var i in Enemy.prototype){
//     BossEnemy.prototype[i] = Enemy.prototype[i];
// }

// 写法2
var a = function (){};
a.prototype = Enemy.prototype;
BossEnemy.prototype = new a();

BossEnemy.prototype.boss_attack = function(){
    console.log("boss_attack");
};
BossEnemy.staticFunc = function(){
    console.log("staticFunc called!");
};

var bos = new BossEnemy();
bos.boss_attack();
bos.attack_play();
BossEnemy.staticFunc();

console.log("==========================");

BossEnemy.prototype.attack_play = function(){
    Enemy.prototype.attack_play.call(this);
    console.log("BossEnemy attack play!");
}
bos.attack_play();

console.log("*****************************");
// 写法三 js6
class BingEnemy extends Enemy{
    constructor(){
        super();
        this.flag = true;
        this.name = "通天教主";
        this.level = 100;
        
    }
    
    static staticFunc(){
        console.log("static func called!");
    }

    get BingName(){
        return this.name;
    }
    set BingName(value){
        this.name = value;
    }
};
BingEnemy.haha ="123";
let bing = new BingEnemy();
console.log(bing);
BingEnemy.staticFunc();
bing.attack_play();
console.log(bing.BingName);
bing.BingName = "jade";
console.log(bing.BingName);
//console.log(BingEnemy.wudi);

console.log("============================");

 

转载于:https://www.cnblogs.com/jadeshu/p/10663572.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值