Advertisement
SergioG_0823849

Untitled

Apr 4th, 2024
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Prototype object for math operations
  2. const MathOperations = {
  3.   // Method to add two numbers
  4.   add(x, y) {
  5.     return x + y;
  6.   },
  7.  
  8.   // Method to subtract two numbers
  9.   subtract(x, y) {
  10.     return x - y;
  11.   }
  12. };
  13.  
  14. // Specific object representing a number value
  15. const NumberValue = {
  16.   value: 0
  17. };
  18.  
  19. // Delegate math operations to the MathOperations prototype methods
  20. NumberValue.__proto__ = MathOperations;
  21.  
  22. // Test math operations
  23. const number1 = Object.create(NumberValue);
  24. number1.value = 10;
  25.  
  26. const number2 = Object.create(NumberValue);
  27. number2.value = 5;
  28.  
  29. console.log(number1.add(5, 3)); // Output: 8
  30. console.log(number2.subtract(10, 3)); // Output: 7
Tags: LEGION
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement