我是否以正确的方式使用mixins实现了模块化javaScript?

时间:2017-06-25 20:14:43

标签: javascript

我正在学习编写基于模块的javaScript编码。 我在阅读javascript模式时遇到了mixins。

因此我试图了解模块化风格的javascript和mixins 在几个例子中实现。

我的例子是雇员,雇员可以是合同雇员或雇员 全职员工。

合同雇员按小时收到工资,而全职工作则获得带薪的月薪。

上述场景的代码如下,

// 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());

从上面的代码可以看出,我已将总薪资计算作为员工的一部分,并且分别与每个员工类型设置薪水。

你能不能通过代码看看我的方法。 我是否能够在我的javaScript代码中实现模块化。

上述编码方式也可以用不同的方式完成,如果是的话,请给我一些例子。

上述代码的输出是

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 

0 个答案:

没有答案