Skip to content

Instantly share code, notes, and snippets.

@stoewer
Created March 10, 2014 08:20
Show Gist options
  • Save stoewer/9461273 to your computer and use it in GitHub Desktop.
Save stoewer/9461273 to your computer and use it in GitHub Desktop.
Javascript OO pattern and inheritance
function isGlobal(other) {
return (function() { return this == other; })();
}
function inherit(object, superclass) {
if (superclass instanceof Function) {
if (arguments.length > 2) {
var param = Array.prototype.slice.call(arguments, 2);
} else {
var param = [];
}
superclass.apply(object, param);
}
return object;
}
function Person(name, famillyname) {
if (isGlobal(this))
return new Person(name, famillyname);
var self = this;
self.name = name;
self.famillyname = famillyname;
self.getName = function() {
return self.famillyname + ", " + self.name;
};
}
Person.prettyPrint = function(person) {
console.log("Name: " + person.name);
console.log("Familly Name: " + person.famillyname);
console.log("");
};
function Employee(name, famillyname, boss) {
if (isGlobal(this))
return new Employee(name, famillyname, boss);
var self = inherit(this, Person, name, famillyname);
var __boss = boss;
self.getBoss = function() {
return __boss;
}
self.setBoss = function(boss) {
__boss = boss;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment