Advertisement
avr39ripe

jsPointClassAdvanced

Mar 18th, 2021
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Study</title>
  6. </head>
  7. <body>
  8.     <script>
  9.         'use strict'
  10.        
  11.         class Point {
  12.  
  13.             static className = "Class Point";
  14.             static objects = 0;
  15.  
  16.             static printObjCount() { console.log(`Class Point was used for: ${Point.objects} objects`); }
  17.  
  18.             constructor(pX, pY) {
  19.                 //let this = {};
  20.                 console.log('Point constructor');
  21.                 this.x = pX;
  22.                 this.y = pY;
  23.                 ++Point.objects;
  24.             }
  25.  
  26.             print() { console.log(`(${this.x},${this.y})`); }
  27.  
  28.             getX() { return this.x; }
  29.  
  30.             getY() { return this.y; }
  31.  
  32.             setX(pX) {
  33.                 if (pX >= 0) {
  34.                     this.x = pX;
  35.                     return true;
  36.                 };
  37.                 return false;
  38.             }
  39.  
  40.             setY(pY) {
  41.                 if (pY >= 0) {
  42.                     this.y = pY;
  43.                     return true;
  44.                 };
  45.                 return false;
  46.             }
  47.  
  48.             move(dX, dY) { this.x += dX; this.y += dY; }
  49.  
  50.         }
  51.  
  52.         class ColorPoint extends Point {
  53.             constructor(pX, pY, pColor = 'red') {
  54.                 console.log('ColorPoint constructor');
  55.                 super(pX, pY);
  56.                 this.color = pColor;
  57.             }
  58.             getColor() { return this.color; }
  59.             setColor(pColor) { this.color = pColor; }
  60.             print() {
  61.                 super.print();
  62.                 console.log(`[${this.color}]`);
  63.             }
  64.         }
  65.  
  66. /*        function Point(pX, pY, pColor = 'red') {
  67.             // let this = {};
  68.             this.x = pX;
  69.             this.y = pY;
  70.             this.color = pColor;
  71.             //[[Prototype]]
  72.  
  73.             // return this;
  74.         }
  75.  
  76.         Point.prototype.print = function () { console.log(`(${this.x},${this.y})`); }
  77.         Point.prototype.getX = function () { return this.x; };
  78.         Point.prototype.getY = function () { return this.y; };
  79.         Point.prototype.setX = function (pX) {
  80.             if (pX >= 0) {
  81.                 this.x = pX;
  82.                 return true;
  83.             };
  84.             return false;
  85.         }
  86.         Point.prototype.setY = function (pY) {
  87.             if (pY >= 0) {
  88.                 this.y = pY;
  89.                 return true;
  90.             };
  91.             return false;
  92.         }
  93.         //Point.prototype.color = 'red';
  94.         Point.prototype.getColor = function () { return this.color; };
  95.         Point.prototype.setColor = function (pColor) { this.color = pColor; };
  96. */
  97.         {
  98.  
  99. /*            let p1 = new Point(21, 22);
  100.             let p2 = new Point(211, 212);
  101.  
  102.             //Point.prototype.move = function (dX, dY) { this.x += dX; this.y += dY; };
  103.             //Point.prototype = {};
  104.             let p3 = new Point(26, 7);
  105.  
  106.             p1.print();
  107.             p1.setX(42);
  108.             p1.move(10, 20);
  109.             p1.print();
  110.             p2.move(4, 4);
  111.             p2.print();
  112.             p2.setY(33);
  113.  
  114.             p1.print();
  115.             p2.print();
  116.  
  117.             p3.print();
  118.             p3.move(3, 3);
  119.             p3.print();
  120.  
  121.             console.log(p1);
  122.             console.log(p2);
  123.             */
  124.         }
  125.  
  126.     </script>
  127. </body>
  128. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement