How does JavaScript .prototype work?

JavascriptWeb DevelopmentFront End Technology

The objects in JavaScript inherit the properties and methods from their prototype. We generally create one with new Object(), which also gets inherit from Object.prototype. In the same way, objects with new Date() inherit Date.prototype.

Example

<!DOCTYPE html>
<html>
   <body>
      <h2>Department Details</h2>
      <p id="details"></p>
      <script>
         function Department(myid, name, section) {
            this.id = myid;
            this.name = name;
            this.section = section;
         }
         var myDept = new Department("AKD", "Marketing", "North");
         document.getElementById("details").innerHTML =
            "The name and section of the Department is " + myDept.name + " and "
            + myDept.section+ " respectively.";
      </script>
   </body>
</html>
raja
Published on 31-Jan-2018 12:42:39
Advertisements