es5和es6聲明類的區別/es5和es6繼承的區別

// es5和es6聲明類的區別,es5沒有統一語法規範。es6有統一寫法規範 start。
// es5聲明「類」的語法--僞類
// function Person(name,age){
//     this.name = name;
//     this.age = age;
//     // this.showName = function(){
//     //     alert(this.name);
//     // };
//     // this.showAge = function(){
//     //     alert(this.age);
//     // }
// }
// Person.prototype.showName = function(){
//     alert(this.name)
// }
// Person.prototype.showAge = function(){
//     alert(this.age)
// }
// let p = new Person('blue',18);
// p.showName();
// p.showAge();

// es6有單獨的聲明類的方法
// class Person{
//     constructor(name,age){
//         this.name = name;
//         this.age = age;
//     }
//     showName(){
//         alert(this.name);
//     }
//     showAge(){
//         alert(this.age);
//     }
// }
// let p = new Person('red',19)
// p.showName();
// p.showAge();
// es5和es6聲明類的區別,es5沒有統一語法規範。es6有統一寫法規範 end。
// es5和es6的繼承區別 ----------------- start
// es5
// function Person(name,age){
//     this.name = name;
//     this.age = age;
// }
// Person.prototype.showName = function(){
//     alert(this.name)
// }
// Person.prototype.showAge = function(){
//     alert(this.age)
// }
// function Worker(name,age,job){
//     Person.call(this,name,age);
//     this.job = job;
// }
// Worker.prototype = new Person()
// Worker.prototype.constructor = Worker;
// Worker.prototype.showJob = function(){
//     alert(this.job);
// };
// let w = new Worker('huihui',2,'大學教授');
// w.showName();
// w.showAge();
// w.showJob();

// es6
class Person{
    constructor(name,age){
        this.name = name;
        this.age = age;
    }
    showName(){
        alert(this.name);
    }
    showAge(){
        alert(this.age);
    }
}
class Worker extends Person{
    constructor(name,age,job){
        super(name,age);
        this.job = job;
    }
    showJob(){
        alert(this.job);
    }
}
let w = new Worker('張景輝','28','大學教授');
w.showName();
w.showAge();
w.showJob();
// 
// es5和es6的繼承區別 ----------------- end

 若是對小哥哥小姐姐有幫助請點個推薦哈,歡迎留言、評論、搞事!!   雙肩揹包 【正品折扣專業店】 -- biy1314.taobao.comes6

相關文章
相關標籤/搜索