avr39ripe

jsPointPrototypeBased

Mar 14th, 2021
189
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.     <div id="output"></div>
  9.     <script>
  10.         'use strict'
  11.  
  12.         {
  13.             function Point(pX, pY) {
  14.                 this.x = pX;
  15.                 this.y = pY;
  16.  
  17.                 Point.prototype.print = function () { console.log(`(${this.x},${this.y})`); }
  18.                 Point.prototype.getX = function () { return this.x; };
  19.                 Point.prototype.getY = function () { return this.y; };
  20.                 Point.prototype.setX = function (pX) {
  21.                     if (pX >= 0) {
  22.                         this.x = pX;
  23.                         return true;
  24.                     };
  25.                     return false;
  26.                 }
  27.                 Point.prototype.setY = function (pY) {
  28.                     if (pY >= 0) {
  29.                         this.y = pY;
  30.                         return true;
  31.                     };
  32.                     return false;
  33.                 }
  34.             }
  35.  
  36.             let p1 = new Point(21, 22);
  37.             /// p1.x = -123545;
  38.             let p2 = new Point(211, 212);
  39.             Point.prototype.move = function (dX, dY) { this.x += dX; this.y += dY; }
  40.             let p3 = new Point(26, 7);
  41.  
  42.             p1.print();
  43.             p1.setX(42);
  44.             p2.print();
  45.             p2.setY(33);
  46.  
  47.             p1.print();
  48.             p2.print();
  49.             p3.print();
  50.  
  51.             p2.move(5, 6);
  52.             p2.print();
  53.             p3.move(5, 6);
  54.             p3.print();
  55.  
  56.             console.log(p1);
  57.             console.log(p2);
  58.             console.log(p3);
  59.             console.log(p1.__proto__ == Point.prototype);
  60.             console.log(p3.__proto__ == Point.prototype);
  61.             console.log(p3.__proto__ == p1.__proto__);
  62.         }
  63.     </script>
  64.     <h1></h1>
  65. </body>
  66. </html>
Advertisement
Add Comment
Please, Sign In to add comment