Created
July 18, 2015 20:14
-
-
Save nvasilakis/8883821dd62ff7199d64 to your computer and use it in GitHub Desktop.
A mini tutorial on JavaScript objects and inheritance
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
/** | |
* A mini tutorial on JavaScript Object Inheritance | |
* | |
* API myPerson = person.create() creates a person object | |
* myPerson.id set/get ID | |
* myPerson.name set/get name | |
* myPerson.age set/get age | |
* myPerson.salute() return a salute (incl. name) | |
* person.staticSalute() returns a static salute (no name) | |
* | |
* API myManager = manager.create() creates a manager object | |
* -- also a person | |
* | |
* myManager.id, myManager.name, myManager.age are inherrited by person | |
* myManager.projects is an array of project names | |
* myManager.expenses is a number | |
* manager.minimumAge() returns the minimum age of managers | |
* | |
* API myBoss = boss.create() creates a _singleton_ boss object | |
*/ | |
/** | |
* Base Object | |
*/ | |
function Person(name, age) { | |
this.name = name || ""; | |
this.age = age || 0; | |
this.id = "this should be random"; | |
} | |
/** | |
* A Base method | |
*/ | |
Person.prototype.salute = function () { | |
return "Hey " + this.name + "! How's everything?"; | |
} | |
/** | |
* A base _static_ method | |
*/ | |
var staticSalute = function() { | |
return "This is a greeting"; | |
} | |
/** | |
* Static factory method -- no use of constructor | |
*/ | |
var createPerson = function(name, age) { | |
return new Person(name, age); | |
} | |
/** | |
* A private field | |
*/ | |
var managerID = 1; | |
/** | |
* A Sub-Object (inherits from person) | |
*/ | |
function Manager(name, age) { | |
that = new Person(name, age); | |
that.projects = []; | |
that.expenses = 0; | |
that.id = managerID++; | |
return that; | |
} | |
/** | |
* Create pointers to Person in the inheritance chain -- for all Manager objects | |
* | |
* TODO: What is the difference between using `new Person(..)` and `Person()` | |
*/ | |
Manager.prototype = Object.create(Person.prototype); | |
/** | |
* ..but make sure the constructor (the object "type") is correct by overriding | |
*/ | |
Manager.prototype.constructor = Manager; | |
/** | |
* Method shared across all manager objects | |
*/ | |
Manager.prototype.minimumAge = function () { | |
return 30; | |
} | |
/** | |
* Static factory for managers | |
*/ | |
var createManager = function(name, age) { | |
return new Manager(name, age) | |
} | |
/** | |
* Boss inherits from Manager | |
*/ | |
function Boss(name, age) { | |
var that = new Manager(name, age); | |
return that; | |
} | |
/** | |
* take care of inheritance chain and "type" (constructor) as above | |
*/ | |
Boss.prototype = Object.create(Manager.prototype); | |
Boss.prototype.constructor = Boss; | |
var ourBoss; //undefined | |
/** | |
* Static factory -- N.b. enforces singleton | |
*/ | |
var createBoss = function(name, age) { | |
if (ourBoss !== undefined) { | |
return ourBoss; | |
} else { | |
ourBoss = new Boss(name, age); | |
return ourBoss; | |
} | |
} | |
// TODO: These should probably be different files? | |
// Either "personel" module | |
exports.Person = {} | |
exports.Manager = {} | |
exports.Boss = {} | |
exports.Person.create = createPerson | |
/** | |
* Static salute is renamed to salute | |
* _Note: static methods are not inherited!_ | |
*/ | |
exports.Person.salute = staticSalute | |
exports.Manager.create = createManager | |
exports.Boss.create = createBoss | |
// TODO: Try importing and using from another file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment