Created
August 4, 2010 20:45
-
-
Save jeffreyolchovy/508768 to your computer and use it in GitHub Desktop.
Inheritance model for 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
function Debug() { | |
var frame = window.open("", "Debugger", "location=no,scrollbars=yes,height=300,width=400"); | |
var console = frame.document.createElement("div"); | |
console.setAttribute("id", "console"); | |
console.style.margin = "auto"; | |
console.style.width = "350px"; | |
console.style.overflow = "auto"; | |
frame.document.body.appendChild(console); | |
this.log = function(str) { | |
var pre = frame.document.createElement("pre"); | |
var code = frame.document.createElement("code"); | |
code.innerHTML = str; | |
var hr = frame.document.createElement("hr"); | |
pre.appendChild(code); | |
frame.document.body.appendChild(pre); | |
frame.document.body.appendChild(hr); | |
frame.focus(); | |
} | |
this.dir = function(obj) { | |
for(var property in obj) this.log("\t" + obj[property]); | |
} | |
}; | |
var debug = (function() { | |
try { return console; } catch(e) { return new Debug(); } | |
})(); | |
(function() { | |
Class = Function(); | |
Class.define = function callee() { | |
var self = this; | |
var constructor = arguments[0] || new Function(); | |
var Class = function() { | |
this.parent = self.prototype; | |
constructor.apply(this, arguments); | |
}; | |
var F = Function(); | |
F.prototype = this.prototype; | |
Class.prototype = new F(); | |
Class.prototype.constructor = Class; | |
Class.extend = callee; | |
Class.mixin = function(Mixin) { | |
for(var method in Mixin.prototype) { | |
this.prototype[method] = Mixin.prototype[method]; | |
} | |
} | |
return Class; | |
} | |
}()); | |
var Person = Class.define( | |
function(name) { | |
this.name = name; | |
} | |
); | |
var Coder = Person.extend( | |
function() { | |
this.parent.constructor.apply(this, arguments); | |
} | |
); | |
var Manager = Person.extend( | |
function(name, salary) { | |
this.parent.constructor.apply(this, arguments); | |
this.salary = "$" + salary + ".00 / Hour"; | |
} | |
); | |
var Suit = Manager.extend( | |
function(options) { | |
var args = []; | |
args.push(options.name); | |
args.push(options.salary); | |
args.push(options.position); | |
this.parent.constructor.apply(this, args); | |
} | |
); | |
var p = new Person("Sam"); | |
var c = new Coder("Bob"); | |
debug.log("Is " + p.name + " a person: " + (p instanceof Person)); | |
debug.log("Is " + p.name + " a coder: " + (p instanceof Coder)); | |
debug.log("Is " + c.name + " a person: " + (c instanceof Person)); | |
debug.log("Is " + c.name + " a coder: " + (c instanceof Coder)); | |
Person.prototype.canTouchType = "no"; | |
debug.log("Can " + p.name + " touch-type?: " + p.canTouchType); | |
debug.log("Can " + c.name + " touch-type?: " + c.canTouchType); | |
debug.log("Are you sure? What about now?"); | |
Coder.prototype.canTouchType = "yes!"; | |
debug.log("Can " + p.name + " touch-type?: " + p.canTouchType); | |
debug.log("Can " + c.name + " touch-type?: " + c.canTouchType); | |
var m = new Manager("Christine", 100); | |
debug.log("Is " + m.name + " a person: " + (m instanceof Person)); | |
debug.log("Is " + m.name + " a coder: " + (m instanceof Coder)); | |
debug.log("Is " + m.name + " a manager: " + (m instanceof Manager)); | |
debug.log("How much money does " + m.name + " make?: " + m.salary); | |
debug.log("Can " + m.name + " touch-type?: " + m.canTouchType); | |
var s = new Suit({"name": "Barry", "position": "COO"}); | |
debug.log("Is " + s.name + " a person: " + (s instanceof Person)); | |
debug.log("Is " + s.name + " a coder: " + (s instanceof Coder)); | |
debug.log("Is " + s.name + " a manager: " + (s instanceof Manager)); | |
debug.log("Is " + s.name + " a suit: " + (s instanceof Suit)); | |
debug.log("How much money does " + s.name + " make?: " + s.salary); | |
debug.log("Can " + s.name + " touch-type?: " + s.canTouchType); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment