Saturday, August 23, 2025

Custom method using prototype in JavaScript.

<!DOCTYPE html>

<html>

<head>

  <title>Custom Prototype Method</title>

</head>

<body>

  <h2>Prototype Sum Method</h2>

  <button onclick="calculate()">Click to Calculate</button>


  <script>

    // ✅ Add custom method using prototype

    Number.prototype.add = function(num) {

      return this + num;

    };


    function calculate() {

      let result = (10).add(20); // using custom method

      alert("The sum is: " + result);

    }

  </script>

</body>

</html>


No comments:

Post a Comment

  1. What is Google Opal? It’s a no-code / low-code AI mini-app builder developed by Google Labs. You describe what you want in natural ...