ES5,ES6(对象与继承的分别)

1.ES5对象

function User(name,pass){
        this.name = name;
        this.pass = pass;
    }
    User.prototype.showNameAndPass = function(){
        console.log(this.name);
        console.log(this.pass);
    }//ES5对象
     let lap = new User("lap","123");
    lap.showNameAndPass();

2.ES6对象

class User{
        constructor(name,pass){
        this.name = name;
        this.pass = pass;
        }
        showNameAndPass(){
            console.log(this.name);
            console.log(this.pass);
        }
    }//ES6对象

 let lap = new User("lap","123");
    lap.showNameAndPass();

3.ES5继承

function otherUser(name,pass,level){
       User.call(this,name,pass);
       this.level = level;
    }
    otherUser.prototype = new User();
    otherUser.prototype.constructor = otherUser;
    otherUser.prototype.showlevel = function(){
        console.log(this.level);
    }
    let laps = new otherUser("laps","456",999);
    laps.showlevel();//ES5继承

4.ES6继承

class otherUser extends User{
        constructor(name,pass,level){
            super(name,pass);
            this.level = level;
        }
        showlevel(){
            console.log(this.name,this.pass,this.level);
        }
    }//ES6继承

    let lap1 = new otherUser("lap1","456",999);
    lap1.showlevel();

猜你喜欢

转载自blog.csdn.net/liuanpingfirst/article/details/81486960