Home > php教程 > php手册 > JavaScript模拟实体类的实现

JavaScript模拟实体类的实现

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:01:12
Original
984 people have browsed it

/* * JavaScript模拟实体类的实现 */ function User(){ this.name = ""; this.age = 0; //如果传入实参直接 初始化 if (arguments.length == 2) { this.name = arguments[0]; this.age = arguments[1]; } this.getName = function(){ return this.name; } th

    /*

     * JavaScript模拟实体类的实现

     */

    function User(){

        this.name = "";

        this.age = 0;

        //如果传入实参直接 初始化

        if (arguments.length == 2) {

            this.name = arguments[0];

            this.age = arguments[1];

        }

        this.getName = function(){

            return this.name;

        }

 

        this.setName = function(name){

            this.name = name;

        }

 

        this.getAge = function(){

            return this.age;

        }

 

        this.setAge = function(age){

            this.age = age;

        }

    }

 

    var user = new User("zdw", 22);

    alert("name:" + user.getName() + ",age : " + user.getAge());

    user.setName("admin");

    user.setAge(99);

    alert("changed********name:" + user.getName() + ",age : " + user.getAge());

source:php.cn
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
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template