Last active
December 17, 2015 13:19
-
-
Save curtis1000/5616437 to your computer and use it in GitHub Desktop.
An example of the Constructor Invocation Pattern in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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