1.类的定义 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 function Student (id, name, score ) { this .id = id; this .name = name; this .score = score; } Student .prototype .display_score = function ( ) { console .log (this .name + ' score = ' + this .score ); } let Tom = new Student (1 , "Tom" , 90 );console .log (Tom );Tom .display_score ();class Student { constructor (id, name, score ) { this .id = id; this .name = name; this .score = score; } display_score ( ) { console .log (this .name + ' score = ' + this .score ); } } let Tom = new Student (1 , "Tom" , 90 );console .log (Tom );Tom .display_score ();
2.类的静态成员 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 function Student ( ) { } Student .name = "John" ;Student .learn = function ( ) { console .log ('study' ); }; Student .prototype .id = 1 ;let s1 = new Student ();console .log (s1.name );s1.learn (); console .log (s1.id );class Student { static name = "John" ; static learn ( ) { console .log ('study' ); }; } let s2 = new Student ();console .log (s2.name );console .log (Student .name );s2.learn (); Student .learn ();
3.类继承 (1) ES5 实现类继承
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 function Phone (brand, price ) { this .brand = brand; this .price = price; } Phone .prototype .sendShortMessage = function ( ) { console .log ("send a short message" ); } function SmartPhone (brand, price, color, size ) { Phone .call (this , brand, price); this .color = color; this .size = size; } SmartPhone .prototype = new Phone ;SmartPhone .prototype .constructor = SmartPhone ;SmartPhone .prototype .takePhoto = function ( ) { console .log ("take a photo" ); } SmartPhone .prototype .playGame = function ( ) { console .log ("play game" ); } const iphone = new SmartPhone ("apple" , 8000 , "black" , "5.5" );console .log (iphone);iphone.sendShortMessage (); iphone.takePhoto (); iphone.playGame ();
(2) ES6 实现类继承
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 class Phone { constructor (brand, price ) { this .brand = brand; this .price = price; } sendShortMessage ( ) { console .log ("send a short message" ); } } class SmartPhone extends Phone { constructor (brand, price, color, size ) { super (brand, price); this .color = color; this .size = size; } takePhoto ( ) { console .log ("take a photo" ); } playGame ( ) { console .log ("play game" ); } } const iphone = new SmartPhone ("apple" , 8000 , "black" , "5.5" );console .log (iphone);iphone.sendShortMessage (); iphone.takePhoto (); iphone.playGame ();
(3) 子类对父类方法的重写
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 class Phone { sendShortMessage ( ) { console .log ("send a short message" ); } } class SmartPhone extends Phone { sendShortMessage ( ) { console .log ("send a image message" ); } } const iphone = new SmartPhone ("apple" , 8000 , "black" , "5.5" );iphone.sendShortMessage ();
注意:子类的方法不可以使用 super 关键字调用父类的同名方法
4.getter 和 setter 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 class Phone { get price () { console .log ("read the attribute price value" ); return "price value" ; } set price (newPrice ) { if (isNaN (newPrice)) { console .log ("the price value must be a number" ); } else { console .log (`set the attribute price value, the new value is ${newPrice} ` ); } } } let p = new Phone ();p.price = 8000 ; p.price = "abc" ; console .log (p.price );