JavaScript Intensive Tutorial - JS Object-Oriented Programming

This article is the official HTML5 training course of the H5EDU organization. It mainly introduces: JavaScript Intensive Course - JS Object-Oriented Programming
  Abstract Description of Things
  Describes the characteristics and behaviors of such things
  Objects are instances of classes
Code implementation: create a class
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+" moving to "+x+","+y);
        }
        this.eat=function(){
            document.write("eating");
        }
    }
class instance: create an object
var p1 = new peple();
    p1 .name="ada";
    p1.hp = 100;
    p1.move(100,100);
    p1.move(22,200);


Dynamically extend the method outside the class
   p1.fire = function(x,y){
        document.write(this.name+" is firing to "+x+","+y);
        p1.hp--;
    }
  
call the function defined outside the Function


p1.fire(00,00);
    p1.fire(43,22);
    p1.fire(66,88);
    document.write("hp="+p1.hp);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326481389&siteId=291194637