Created
May 8, 2010 14:54
-
-
Save jeena/394599 to your computer and use it in GitHub Desktop.
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
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