Created
March 10, 2014 08:20
-
-
Save stoewer/9461273 to your computer and use it in GitHub Desktop.
Javascript OO pattern 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
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