Created
June 13, 2020 19:59
-
-
Save datduyng/9aa8c3075b08e99cdf158736f6abeab0 to your computer and use it in GitHub Desktop.
Revisions
-
datduyng created this gist
Jun 13, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,126 @@ <!DOCTYPE html> <html> <body> <h2>JavaScript Class</h2> <p>In this example we demonstrate a simple class definition and how to use it.</p> <p id="demo"></p> <script> /* Class - a convinient way to group information. group properties and method List - a dynamic array https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array let list = []; list.push('item1'); list.push('item2'); console.log(list); //['item1','item2'] Array - a static array */ class Car{ constructor(id,brand, fuelType, bodyType, price) { this.id = id; this.brand = brand; this.fuelType = fuelType;//electric, gas, man-power this.bodyType = bodyType; this.price = price; } run(speed) { console.log('runnning with speed ', speed); } } //create a new Car Class // let car = new Car('bmw', 'metal', 'metal', 200); class ShopTouristInfo{ constructor() { this.note = "no climbing"; } } class CarShop{ constructor(name, address, startAmount) { this.name = name; this.address = address; this.revenue = startAmount; this.numSold = 0; this.carList = []; this.touristInfo = new ShopTouristInfo(); } sell(id, sellPrice){ //iterate through car list to check if car exist by id let carIndex = -1; for (let i=0; i<this.carList.length; i++) { let car = this.carList[i]; if (id == car.id) { carIndex = i; break; } } if (carIndex == -1) { //not found in carList console.log ("Not found car with id", id); return this; } else { // found // 2. increase revenue by price this.revenue = this.revenue + sellPrice; // 3. remove car from carList this.carList.splice(carIndex, 1); } return this; } buy(car){ if (this.revenue - car.price < 0) { console.log("Hey loser, you don't have enough money to buy ", car.brand); return this; } this.carList.push(car); this.revenue = this.revenue - car.price; return this; } } // run ShopBicycle // User using ShopBicycle let shop = new CarShop('Avenger', 'New york', 500); //bmw, tesla, toyota let bmw_i7_1 = new Car(1, 'bmw', 'gas', 'metal', 100); let bmw_i3_1 = new Car(2, 'bmw', 'electric', 'chrome', 200); let bmw_i3_2 = new Car(3, 'bmw', 'electric', 'chrome', 200); let tesla_modelx_1 = new Car(4, 'tesla', 'electric', 'chrome', 200); let tesla_model3_1 = new Car(5, 'tesla', 'man-power', 'rubber', 50); let tesla_model3_2 = new Car(7, 'tesla', 'man-power', 'rubber', 50); shop.buy(tesla_model3_1) .buy(tesla_model3_2); shop.buy(tesla_modelx_1); shop.buy(bmw_i7_1); shop.buy(bmw_i3_1); shop.buy(bmw_i3_2); shop.sell(4, 1000); shop.sell(1, 500); </script> </body> </html>