Created
March 18, 2011 16:30
-
-
Save vinbarnes/876370 to your computer and use it in GitHub Desktop.
JavaScript Prototype “Class” Manager
This file contains hidden or 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
/* === MANAGER CLASS === */ | |
var Manager = function(cons) { | |
this.cons = cons; | |
this.objs = []; | |
}; | |
Manager.prototype.create = function(args) { | |
var inst = new this.cons(args) | |
this.objs.push(inst); | |
return inst; | |
}; | |
Manager.prototype.all = function() { | |
return this.objs; | |
}; | |
Manager.prototype.findBy = function(p, v) { | |
for(var i = 0, len = this.objs.length; i < len; i++) { | |
if(this.objs[i][p] === v) { | |
return this.objs[i]; | |
} | |
} | |
}; | |
Manager.prototype.remove = function(o) { | |
for(var i = 0, len = this.objs.length; i < len; i++) { | |
if(this.objs[i].id === o.id) { | |
this.objs.splice(i,1); | |
i = this.objs.length; | |
} | |
} | |
}; | |
/*** | |
var Person = function() { | |
this.init(); | |
} | |
var AddressBook = new Manager(Person); | |
AddressBook.create({FirstName: “Bob”}); | |
***/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
via http://taitems.tumblr.com/post/1654411534/javascript-prototype-class-manager