JavaScript强化教程-JS面向对象编程 ...

本文为H5EDU机构官方HTML5培训教程,主要介绍:JavaScript强化教程——JS面向对象编程
  对事物的抽象描述
  描述这类事物的特征和行为
  对象是类的实例
代码实现:创建一个类
 function peple(){
       this.hp=0;
        this.act = 30;
        this.name = "";
        this.x=0;
        this.y=0;
        this.move =function(x,y){
            document.write(this.name+"正在移动到"+x+","+y);
        }
        this.eat=function(){
            document.write("正在吃");
        }
    }
类的实例:创建一个对象
 var p1 = new peple();
    p1.name="阿达";
    p1.hp = 100;
    p1.move(100,100);
    p1.move(22,200);


 [b]在类外部动态扩展方法[/b]
   p1.fire = function(x,y){
        document.write(this.name+"正在开火向"+x+","+y);
        p1.hp--;
    }
   
调用函数外部定义的函数


 p1.fire(00,00);
    p1.fire(43,22);
    p1.fire(66,88);
    document.write("hp="+p1.hp);
发布了25 篇原创文章 · 获赞 3 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/wanda000/article/details/51775712