Skip to content

Instantly share code, notes, and snippets.

@vinbarnes
Created March 18, 2011 16:30
Show Gist options
  • Save vinbarnes/876370 to your computer and use it in GitHub Desktop.
Save vinbarnes/876370 to your computer and use it in GitHub Desktop.
JavaScript Prototype “Class” Manager
/* === 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”});
***/
@vinbarnes
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment