js面向对象

in js 代码 with 0 comment

简单的面向对象:

<script>

    function Person() {
        this.realname="Person RealName";
    }

    function User(username,birthday,gender) {

        Person.apply(this,arguments);
        this.username=username;
        this.birthday=birthday;
        this.gender=gender;

    }
    User.prototype.getUsername=function () {
        return "name is :"+this.username;
    };

    function h() {
        User.prototype=new Person();
        let justmyvar=new User("小明",1573525293,1);
        let anothervar=new User("小红",1573525093,0);

        justmyvar.realname="张啸明";
        console.log(justmyvar.realname);
        console.log(anothervar.realname);

    }
</script>

参考:
Javascript 面向对象编程(一):封装
JavaScript面向对象简介

Responses