Home > Web Front-end > JS Tutorial > JavaScript Enhancement Tutorial-JS Object-Oriented Programming

JavaScript Enhancement Tutorial-JS Object-Oriented Programming

黄舟
Release: 2017-01-21 16:22:50
Original
1130 people have browsed it

Abstract description of things
Describe the characteristics and behavior of such things
Objects are instances of classes
Code implementation: Create a class

1

2

3

4

5

6

7

8

9

10

11

12

13

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("正在吃");

        }

    }

Copy after login

Instance of the class: Create an object

1

2

3

4

5

var p1 = new peple();

    p1.name="阿达";

    p1.hp = 100;

    p1.move(100,100);

    p1.move(22,200);

Copy after login
Dynamic extension method

1

2

3

4

p1.fire = function(x,y){

        document.write(this.name+"正在开火向"+x+","+y);

        p1.hp--;

    }

Copy after login
outside the class

Call the function defined outside the function

1

2

3

4

p1.fire(00,00);

    p1.fire(43,22);

    p1.fire(66,88);

    document.write("hp="+p1.hp);

Copy after login

The above is the content of JavaScript Enhanced Tutorial-JS Object-Oriented Programming, more related content Please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template