Skip to content

Instantly share code, notes, and snippets.

@alloyking
Forked from curtis1000/ConstructorInvocation.js
Created October 14, 2013 20:31
Show Gist options
  • Save alloyking/6981728 to your computer and use it in GitHub Desktop.
Save alloyking/6981728 to your computer and use it in GitHub Desktop.
/**
* Constructor Invocation Pattern
* Requires use of the "new" keyword, allows for inheritance, but no information hiding
* The Functional Pattern is recommended over the Constructor Invocation Pattern
*/
// 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 curt = new Employee({name: 'Curtis Branum', employeeId: 207});
console.log(curt);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment