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.com


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM