1. 程式人生 > 其它 >ES5建構函式寫法與ES6建構函式寫法

ES5建構函式寫法與ES6建構函式寫法

ES5 建構函式

      function Person(){
            this.name='建林';
            this.age=18;
            this.say=function(){
                console.log('person的say')
            }
        }
        let p1=new Person();
        console.log(p1.name);
        p1.say();
View Code

ES6 建構函式與繼承

        //ES6語法 class
class Person { constructor() { this.name = "建林"; this.age = 18; } say() { console.log('person的say') } } let p2 = new Person(); console.log(p2.name); p2.say();
//ES6建構函式繼承 class Child extends Person{ //複雜寫法 // constructor(){ // //繼承必須加super() // super(); // this.sex="男"; // this.name="思聰";//繼承覆蓋了 // this.score=1000; // } //簡單寫法 sex='男'; name
='思聰'; score=1000; hello(){ console.log('hello'); } abc(){ console.log(abc); } } let p3=new Child(); console.log(p3); console.log(p3.name); console.log(p3.say()); console.log(p3.hello());
View Code