SHARE
TWEET

Untitled

a guest Jun 20th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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');
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top