{ // 基本定義和生成實例 class Parent{ constructor(name='mukewang'){ this.name=name; } } let v_parent1=new Parent(); let v_parent2=new Parent('v'); console.log(... ...
{ // 基本定義和生成實例 class Parent{ constructor(name='mukewang'){ this.name=name; } } let v_parent1=new Parent(); let v_parent2=new Parent('v'); console.log('構造函數和實例',v_parent1,v_parent2); // Parent {name: "mukewang"};Parent {name: "v"} } { // 繼承 class Parent{ constructor(name='mukewang'){ this.name=name; } } class Child extends Parent{ } console.log('繼承',new Child());//Child {name: "mukewang"} } { // 繼承傳遞參數 class Parent{ constructor(name='mukewang'){ this.name=name; } } class Child extends Parent{ constructor(name='child'){ super(name); this.type='child'; } } console.log('繼承傳遞參數',new Child('hello')); //_Child {name: "hello", type: "child"} } { // getter,setter class Parent{ constructor(name='mukewang'){ this.name=name; } get longName(){ return 'mk'+this.name } set longName(value){ this.name=value; } } let v=new Parent(); console.log('getter',v.longName);//mkmukewang v.longName='hello'; console.log('setter',v.longName);//mkhello } { // 靜態方法 class Parent{ constructor(name='mukewang'){ this.name=name; } static tell(){ console.log('tell'); } } Parent.tell(); //tell } { // 靜態屬性 class Parent{ constructor(name='mukewang'){ this.name=name; } static tell(){ console.log('tell'); } } Parent.type='test'; console.log('靜態屬性',Parent.type); //test }