Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. class Car {
  2. constructor(color) {
  3. this.color = color;
  4. this.speed = 0;
  5. }
  6. //方法
  7. run(speed) {
  8. //會更新實例的speed屬性值
  9. this.speed = speed;
  10. console.log('car run'+ this.speed);
  11. },
  12. };
  13.  
  14. //創建實體car
  15. // 參數傳入給constructor
  16. const car1 = new Car('red');
  17.  
  18. //取得color屬性資料
  19. const color = car1.color; // color 等於 red
  20.  
  21. //給color賦值
  22. car1.color = blue;
  23.  
  24. //呼叫方法
  25. car1.run(50); // car run 50
  26.  
  27.  
  28. class ElectricCar extends Car {
  29. constructor(color) {
  30. super(color)
  31. }
  32. };
  33. //當呼叫子類別時,會去執行super(參數),並去執行父類別的constructor,同時把參數傳給父類別的constructor
  34. const ElectricCar1 = new ElectricCar('yellow');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement