DerioFT

index.html

Oct 3rd, 2021 (edited)
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.42 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <title>More Object with Class</title>
  6. </head>
  7. <body>
  8.  
  9.     <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  10.     <script type="text/javascript">
  11.        
  12.         //Car Constructor
  13.         class Car { // 2015 ES6 JS
  14.             constructor (x, y){ // def __init__
  15.                 this.x = x;
  16.                 this.y = y;
  17.                 this.odometer = 0;
  18.             }
  19.  
  20.             draw() { // def draw
  21.                 let carHtml = '<img src="img/car.png">';
  22.  
  23.                 this.carElement = $(carHtml);
  24.  
  25.                 this.carElement.css({
  26.                     position: "absolute",
  27.                     left: this.x,
  28.                     top: this.y
  29.                 });
  30.  
  31.                 $("body").append(this.carElement);
  32.             }
  33.  
  34.             moveRight(){
  35.                 this.x += 100
  36.  
  37.                 this.carElement.css({
  38.                     left: this.x,
  39.                     top: this.y
  40.                 });
  41.             }
  42.  
  43.             moveLeft(){
  44.                 this.x += -30
  45.  
  46.                 this.carElement.css({
  47.                     left: this.x,
  48.                     top: this.y
  49.                 });
  50.             }
  51.  
  52.             moveUp(){
  53.                 this.y += 50
  54.  
  55.                 this.carElement.css({
  56.                     left: this.x,
  57.                     top: this.y
  58.                 });
  59.             }
  60.             moveDown(){
  61.                 this.y += -50
  62.  
  63.                 this.carElement.css({
  64.                     left: this.x,
  65.                     top: this.y
  66.                 });
  67.  
  68.             }
  69.         };
  70.  
  71.         Car.prototype.countingOdometer = function () {
  72.             this.odometer ++;
  73.             console.log(`Car : ${this.odometer} Kms.`)
  74.         }
  75.  
  76.         let tesla = new Car(10, 20);
  77.         let toyota = new Car(100, 200);
  78.  
  79.         tesla.draw();
  80.         tesla.moveRight();
  81.         tesla.moveRight();
  82.         tesla.countingOdometer();
  83.         toyota.draw();
  84.  
  85.     </script>
  86. </body>
  87. </html>
Add Comment
Please, Sign In to add comment