zoukankan      html  css  js  c++  java
  • 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();
    
    //写一个继承函数
  • 相关阅读:
    day03 bs4解析库
    day02—selenium库
    day01爬虫三部曲
    IIC SPI UART通信方式的区别
    五大类程序设计模式
    套接字编程基础
    主机字节序和网络字节序转换
    位运算
    ARM体系结构的特点
    static关键字的作用
  • 原文地址:https://www.cnblogs.com/iflii/p/10191173.html
Copyright © 2011-2022 走看看