Skip to content

Instantly share code, notes, and snippets.

@curtis1000
Last active December 17, 2015 13:19
Show Gist options
  • Save curtis1000/5616437 to your computer and use it in GitHub Desktop.
Save curtis1000/5616437 to your computer and use it in GitHub Desktop.
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment