i apologize because topic comes lot, have not been able have adequately explained in i've read today.
i trying make simple collection class (and learn javascript prototyping @ same time) designed store objects "name" property , lets members accessed index or value. far i've got this:
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]; } };
the problem:
var foo = new collection(); foo.add({name: "someitem", value:"hello world"}); // foo.count()== 1 var bar= new collection(); bar.add({name: "someotheritem", value:"hello world"}); // bar.count()== 2
hmm...
basically, new instance bar
created properties having current values of data in foo
. know can fix putting _xref, etc. inside constructor, i'm trying understand how prototyping works. if create new instance, , make changes data in instance, why values carry on when create new instance?
if make further changes property prototype of foo
or bar
independent, doesn't seem if i'm somehow referencing same instance of anything. causing bar
instantiated current values foo
?
consider classroom full of students. putting on prototype putting on white board them see. when you're declaring
collection.prototype._innerlist = [];
you're giving every collection property; regardless of calling new collection()
changes white board affects students. however, if define within constructor, or 1 of functions this.variablename = []
, each copy have own variablename, handing each student handout. obviously, there's cases when it's okay have on white board, such instructions universal student student, if each item going different each student, should individual property. hope explanation makes sense...
you want doing this.
function collection() { if (!this instanceof collection) return new collection(); this._innerlist = []; this._xref = {}; return this; } 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"}); console.log(foo.count()); // 1 var bar= new collection(); bar.add({name: "someotheritem", value:"hello world"}); console.log(bar.count()); // 1
edit
not relevant question, it's throw out there. whenever i'm doing on prototype, if i'm not returning something, return this
. allows chaining, instance.function1().function2().function3()
long function1
, function2
return this
.
Comments
Post a Comment