JS__class 入坑 chrome測試class 父子類 繼承關系
阿新 • • 發佈:2018-09-07
chrom ret ons undefined per def 繼承關系 color xtend
emmm...直接通過測試來看父子關系吧:
class A{ constructor(){//並非必須 this.name="class_A"; //console.log(this) //A {name: "class_A"} (另起一行)B {name: "class_A"} //父類子類分別執行了一次 } static staticCallNameA(){ return "name_A"; } callNameA(){ return "name_A"; } static cover(){ return"cover_A" } } class B extends A{ constructor(){//子類可以不加 super();//子類必須有 //console.log(this)//B {name: "class_A"} //會直接打印 } static staticCallNameB(){ return "nameB"; } callNameB(){ return "name_B"; } static cover(){ return "cover_B" } }
console.log(A.name,B.name) //A B //(類名) console.log(A.staticCallNameA(),B.staticCallNameA()) //static子類繼承父類 console.log(A.cover(),B.cover()) //cover_A cover_B //先讀子類再讀父類 console.log(A.staticCallNameB) //undefined //父類不具備子類特有函數 console.log(A.callNameA,B.callNameA) //undefined undefined //非static方法類不能直接調用 let a=new A(),b=new B; //加不加括號效果一樣 console.log(a.name); //class_A console.log(a.name,b.name) //class_A class_A console.log(a.callNameA(),a.callNameB,b.callNameB(),b.callNameA()) //name_A undefined name_B name_A //說明繼承關系同上
歡迎各位大神指點...
JS__class 入坑 chrome測試class 父子類 繼承關系