0

I am learning to write modular based javaScript coding. I came across mixins while reading javascript patterns.

Hence I am trying to understand modular style javascript and mixins by implementing in a few examples.

My example has Employee where an employee can be a contractual employee or a full time employee.

Contractual employee get paid on hourly basis, whereas an full time get monthly salary with benefits.

the code for the above scenario is as follows,

// and employee module which is common
var employeeModule = (function(){
    // private variables
    var name = "",
        address = "",
        salary = 0,     
        module = {};

    // publicly available methods
    module.calculateSalary = function(unitOfWork,employeeType){
        var totalPay = 0, perDayWage = 0;

        if(employeeType === "CONTRACTUAL"){
            totalPay = unitOfWork * salary;
        }

        if(employeeType === "FULLTIME"){ // full time employee also get benifits
            perDayWage = salary/30;
            totalPay = (perDayWage * unitOfWork) + (perDayWage * 8); // 8 days of leaves per month
            totalPay += 2300; // monthly allowance for a fulltime employee 
        }

        return totalPay;
    }

    module.setSalary = function(_salary){
        salary = _salary;
    }

    module.getName = function(){
        return name;
    }

    module.setName = function(_name){
        name = _name;
    }

    module.getAddress = function(){
        return address;
    }

    module.setAddress = function(addr){
        address = addr;
    }

    module.init = init;

    return module;


    function init(){
        name = "Rahul";
        salary = 2500;
        address = "India";
    }   
})();


// a contractual employee module
var contractualEmployeeModule = (function(emp){
    var noOfHr = 0, // total number of hours worked 
        payableSalary = 0; // total hourly pay
    var module = {};

    // number of hours an contractual employee worked 
    module.setNoOfHrWorked = function(_noOfHr){
        noOfHr = _noOfHr;
    }

    module.getTotalSalary = function(){
        payableSalary = emp.calculateSalary(noOfHr,"CONTRACTUAL");
        return payableSalary;
    }

    // salary rate for per hour work
    module.setHourlyRate = function(rate){
        emp.setSalary(rate);
    }

    module.setAddress = function(_address){
        emp.setAddress(_address);
    }

    module.setName = function(_name){
       emp.setName(_name);
    }

    module.init = function(){
       emp.init();
    }

    module.getTotalInfo = function(){
        var str = "";
        str += "Name \""+emp.getName() + "\" " +
            "living in \""+ emp.getAddress() +"\""+
            " is contractual employee has earned "+this.getTotalSalary();


        return str;
    }

    return module;

})(employeeModule);

// a fulltime employee module
var fulltimeEmployeeModule = (function(emp){
    var noOfDays = 0, // number of days employee attended for work 
        payableSalary = 0; // total monthly salary an employee is eligible to earn
    var module = {};

    // number of hours an employee worked in a month 
    module.setNoOfDaysWorked = function(_noOfDays){
        noOfDays = _noOfDays;
    }

    // calculating total monthly salary
    // a fulltime employee gets
    module.getTotalSalary = function(){
        payableSalary = emp.calculateSalary(noOfDays,"FULLTIME");
        return payableSalary;
    }

    // total monthly salary an fulltime employee
   // should earn
   module.setMonthlySalary = function(salary){
        emp.setSalary(salary);
   }

   module.setAddress = function(_address){
        emp.setAddress(_address);
   }

   module.setName = function(_name){
        emp.setName(_name);
   }

   module.init = function(){
        emp.init();
   }

   module.getTotalInfo = function(){
        var str = "";
        str += "Name \""+emp.getName() + "\" " +
            "living in \""+ emp.getAddress() +"\""+
            " is a fulltime employee has earned "+this.getTotalSalary();


        return str;
   }

   return module;

 })(employeeModule);


 contractualEmployeeModule.setName("John William");
 contractualEmployeeModule.setAddress("New York");
 contractualEmployeeModule.setHourlyRate(12);
 contractualEmployeeModule.setNoOfHrWorked(123);
 console.log(contractualEmployeeModule.getTotalInfo());

 fulltimeEmployeeModule.setName("Jack Harrison");
 fulltimeEmployeeModule.setAddress("Sedney");
 fulltimeEmployeeModule.setMonthlySalary(2300);
 fulltimeEmployeeModule.setNoOfDaysWorked(25);
 console.log(fulltimeEmployeeModule.getTotalInfo());

From the above code you can see that I have kept total salary calculation as part of employee, and respective to setting salary with each of the employee type.

Can you please go though the code and have a look at my approach. Whether I am able to achieve modularity in my javaScript code.

Also the above way of coding can be done in a different way, if yes can you please give me some example.

The output of the above code is

E:\RahulShivsharan\MyPractise\DesignPatternsInJavaScript\modules>node ex03.js
Name "John William" living in "New York" is contractual employee has earned 1476
Name "Jack Harrison" living in "Sedney" is a full time employee has earned 4830 
Rahul Shivsharan
  • 2,481
  • 7
  • 40
  • 59
  • You want us to review your code? In that case you'd better ask your question at [Code Review](https://codereview.stackexchange.com/). See also [this Q&A](https://stackoverflow.com/questions/17631517/javascript-mixins-when-using-the-module-pattern). – trincot Jun 25 '17 at 20:20
  • The most obvious problem is that the module pattern is inappropriate here. You've implemented singletons, you cannot instantiate multiple employees with this approach. – Bergi Jun 25 '17 at 20:53

0 Answers0