频道栏目
首页 > 资讯 > HTML/CSS > 正文

ES5和ES6面向对象的写法(代码实例分析)

18-05-31        来源:[db:作者]  
收藏   我要投稿

面向对象:

ES5:

 function User(name,age){
                    this.name = name;
                    this.age = age;
        }

        User.prototype.showName=function(){
                console.log(this.name);
        }
        User.prototype.showAge=function(){
                console.log(this.age);
        }

        function Vipuser(name,age,level){
                    User.call(this,name,age);
                    this.level = level;
        }
        Vipuser.prototype = new User();
        Vipuser.prototype.constructor = Vipuser;

        Vipuser.prototype.showLevel=function(){
                console.log(this.level);
        }

        var v1 = new Vipuser('hum',12,3);
        v1.showName();
        v1.showAge();
        v1.showLevel();
ES6:      面向对象:注意User后面没有括号。
class User{
        constructor(name,age){
            this.name = name;
            this.age = age;
        }
        showName(){
            console.log(this.name);
        }
        showAge(){
            console.log(this.age);
        }
     }

     class Vipuser extends User{
        constructor(name,age,level){
            super(name,age);
            this.level = level;
        }
        showLevel(){
            console.log(this.level);
        }
     }
var v1 = new Vipuser('han',10,2);
v1.showName();
v1.showAge();
v1.showLevel();
相关TAG标签
上一篇:HTTP响应报文结构分析讲解
下一篇:VMWare12下安装Centos7 、Mysql8 集群,实现读写分离、高可用的方法
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站