Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Car {
- constructor(color) {
- this.color = color;
- this.speed = 0;
- }
- //方法
- run(speed) {
- //會更新實例的speed屬性值
- this.speed = speed;
- console.log('car run'+ this.speed);
- },
- };
- //創建實體car
- // 參數傳入給constructor
- const car1 = new Car('red');
- //取得color屬性資料
- const color = car1.color; // color 等於 red
- //給color賦值
- car1.color = blue;
- //呼叫方法
- car1.run(50); // car run 50
- class ElectricCar extends Car {
- constructor(color) {
- super(color)
- }
- };
- //當呼叫子類別時,會去執行super(參數),並去執行父類別的constructor,同時把參數傳給父類別的constructor
- const ElectricCar1 = new ElectricCar('yellow');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement