curtis1000
5/20/2013 - 11:33 PM

An example of the Constructor Invocation Pattern in JavaScript

An example of the Constructor Invocation Pattern in JavaScript

/**
 * Constructor Invocation Pattern
 * Requires use of the "new" keyword, allows for inheritance, but no information hiding
 */

// create a constructor function for the base class
var Person = function (spec) {
    // pseudo-class vars
    spec.name = spec.name || '';
    this.name = spec.name;
};

// create a constructor function for the child class
var Employee = function (spec) {
    // call parent constructor
    Person.call(this, spec);
    // pseudo-class vars
    spec.employeeId = spec.employeeId || 0;
    this.employeeId = spec.employeeId;
};

var gob = new Employee({name: 'Gob Bluth', employeeId: 207});

console.log(gob);