Advertisement
eimkasp

Basic class example javascript #2 Cars

Aug 2nd, 2016
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Car = function(engine = 5, weight = 100, fuel = 1 , speed = 0, carValue = 1000) {
  2.     this.engine = engine;
  3.     this.weight = weight;
  4.     this.fuel = fuel;
  5.     this.speed = speed;
  6.     this.damage = 1;
  7.     this.carValue = carValue;
  8. };
  9.  
  10. Car.prototype.race = function() {
  11.     this.speed = this.engine/this.weight * this.fuel;
  12.     this.fuel -= 0.2;
  13. }
  14.  
  15. Car.prototype.refuel = function() {
  16.     this.fuel = 1;
  17. }
  18.  
  19. Car.prototype.crash = function() {
  20.     this.damage = Math.floor(Math.random() * (100 - 0 + 1)) + 0;
  21.     this.carValue -= 100 * this.damage;
  22. }
  23.  
  24. Car.prototype.sell = function() {
  25.     this.value = 100;
  26. }
  27.  
  28. var mercedes = new Car();
  29. var honda = new Car();
  30. var volvo = new Car(1, 1000, 0.5);
  31.  
  32.  
  33. // Mercedes zaidejas nusipirko patobulinima
  34. mercedes.weight = 70;
  35.  
  36.  
  37. honda.race();  // 0,05 greitis
  38. mercedes.race(); // 0,07 greitis
  39.  
  40. if(honda.speed > mercedes.speed) {
  41.     console.log("honda laimejo");
  42.     honda.race();
  43.     volvo.race();
  44. } else {
  45.     console.log("mercedes laimejo");
  46.     mercedes.race();
  47.     volvo.race();
  48. }
  49.  
  50. volvo.crash();
  51.  
  52.  
  53. console.log(honda);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement