Skip to content

Instantly share code, notes, and snippets.

@jeena
Created May 8, 2010 14:54
Show Gist options
  • Save jeena/394599 to your computer and use it in GitHub Desktop.
Save jeena/394599 to your computer and use it in GitHub Desktop.
var DatabaseFactory = {
getInstanceById: function(id, type) {
if(!this.instances) this.instances = {};
var theId = id + "" + typeof type;
if(this.instances[theId]) {
return this.instances[theId];
} else {
this.instances[theId] = new type(id);
return this.instances[theId];
}
}
}
// Database object
function Database(id) {
this.id = id;
}
Database.prototype.save = function() {
console.log("save");
}
Database.prototype.delete = function() {
console.log("delete " + this.id);
}
// User Object
function User(id) { this.constructor(id) };
User.prototype = new Database();
User.prototype.parent = Database.prototype;
User.prototype.test = function() {
console.log("test");
}
User.prototype.delete = function() {
console.log("delete user");
this.parent.delete.call(this);
}
// Test
var myObj = DatabaseFactory.getInstanceById(2, User);
myObj.save();
myObj.test();
console.log(myObj.id);
myObj.delete();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment