factory pattern to create Object - Node.js Object

Node.js examples for Object:Object Operation

Description

factory pattern to create Object

Demo Code


function Employee( options ) {
    this.firstName = options.firstName;/*from ww  w  . j av  a2 s.c o m*/
    this.lastName = options.lastName;
    this.wage = options.wage;
    this.hireDate = options.hireDate;
    this.contactPhone = options.contactPhone;
}


function EmployeeFactory() {}
EmployeeFactory.prototype.createEmployee = function createEmployee ( options ) {
    return new Employee( options );
}

var anEmployeeFactory = new EmployeeFactory();
var newHire = anEmployeeFactory.createEmployee(
    {
        firstName : "Michael",
        lastName : "Fish",
        wage : 13.50,
        hireDate : "05/05/15"
    }
);

var raise_wage = function(employee, raise) {
    employee.wage += raise;
};

raise_wage(newHire, 1);

console.log(newHire.wage);

console.log(newHire.contactPhone);

Related Tutorials