On Dec 22, 11:14 pm, אריה גלזר <arieh.gla...@gmail.com> wrote: > But if you want a 'private' variable for each instance, the only way you are > going to achieve this (I think) is by creating a separate closure for each > object creation. So you can either do the above second solution, or you can > do > var obj = { > getA : function getA() { > return this.a; > }, > setA : function setA(b) { > a = this.b; > } > }; > > function F(){ this.a = 'a';} > for (i =0; i<10;i++) x.push((function(){ F.prototype = obj; return new > F();})(); > There is no reason to set the prototype of F in a closer inside each iteration of the loop. You can set that up before using it in the loop like this:
function F() { this.a = 'a'; } F.prototype = { getA : function () { return this.a; }, setA : function (b) { this.a = b; } }; var x = [], i = 0; for (i = 0; i < 10; i +=1) { x.push(new F()); x[i].setA("Name " + i); } for (i = 0; i < 10; i +=1) { console.log(x[i].getA()); } // Prints out Name 0, Name 1,... Name 9 - Jordan Harrison -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com