Here is a way to create class in Javascript

By xngo on February 26, 2019

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <script type="text/javascript">
       * Here is a way to create class in Javascript. There are many other ways.
       * Note: I explicitly omit to show private members as it is getting complicated.
       *        For simplicity, stick with the public members. 
       *        For more info, see
      // Employee class
      function Employee(sName)
        // Public variables. = sName;
        this.iWorkHours = 5;
      // Implementation of functions.
      Employee.prototype.addWorkHours = function(iHours)
        this.iWorkHours += iHours;
      Employee.prototype.getWorkHours = function()
        return this.iWorkHours;
      // Create Employee object.
      var emplA = new Employee();
      // Direct access to public variable."Xuan Ngo";
      // Calling function.
      // Display the results.
      window.alert( ' worked for ' +emplA.getWorkHours()+ ' hrs.');
    <title>Class example</title>

About the author

Xuan Ngo is the founder of He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.