JSFiddle - React, Tailwind, and code Playground
JavaScript
function Collection() {}
Collection.prototype.constructor = Collection;
Collection.prototype._innerList = [];
Collection.prototype._xref = {};
Collection.prototype.count = function () { return this._innerList.length; };
Collection.prototype.add = function (obj) {
this._xref[obj.name] = this._innerList.push(obj) - 1;
}
Collection.prototype.get = function (id) {
if (typeof id == "string") {
return this._innerList[this._xref[id]];
} else {
return this._innerList[id];
}
};
var foo = new Collection();
foo.add({name: "someitem", value:"hello world"});
document.write(foo.count()); // ==> 1
document.write("<br>");
var bar= new Collection();
bar.add({name: "someotheritem", value:"hello world"});
document.write(bar.count()); // ==> 2
document.write("<br>-- done");