類 class編程
ES6 提供了更接近傳統語言的寫法,引入了 Class(類)這個概念,做爲對象的模板。this
經過 class 關鍵字,能夠定義類 classspa
新的 class 寫法只是讓對象原型的寫法更加清晰、更像面向對象編程的語法而已prototype
類的全部方法都定義在類的 prototype
屬性上面,constructo() 也是類原型的 constructor()code
class Point { constructor(x, y) { /* 構造方法,實例 = new 類 */ this.x = x; this.y = y; }; toString() { return '(' + this.x + ', ' + this.y + ')'; }; }; /**** 子類 繼承 父類 ****/ class myPoint extends Person { constructor(x, y, color){ /* 構造方法若是不寫,系統默認也會建立,並在內調用 super() */ super(x, y); /* 調用父類的構造方法,繼承父類的 屬性 和 方法 */ this.color = color; }; sayIt(){ /* 再也不須要 function 關鍵字 */ return 'This is a '+color+'point('+x+', '+y+')'; }; };
4對象
5blog
5繼承
5原型
5io
5
5
5
5
5
5
5
5
5
5
55
5
5
5
5
5
5
5
5
5
55
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5