> var counter = new function() {
> var i = 0;
> this.next = function() {
> return ++i;
> };
> this.reset = function() {
> return (i = 0);
> };
> };
Why would you create a new function for every new instance? Why not just use
prototype to begin with?
var A = function(){};
A.prototype = {
i: 0,
next: function(){
return ++this.i;
},
reset: function(){
this.i = 0;
}
};
var a = new A;
a.next();
a.reset();
Unless of course you _really_ don't want to polute the object (or prototype)
with the i property. Or when you still think you can safely hide values in js.
(Because in that case you do need to instantiate new functions for every
instance since you need unique counters for each instance).
In most cases, the i property is perfectly acceptable though.
(Note that it's not really a matter of optimization, because the difference
will
hardly be noticable, if at all, but more one of clean code)
- peter
_______________________________________________
JSMentors mailing list
[email protected]
http://jsmentors.com/mailman/listinfo/jsmentors_jsmentors.com
List Archive:
http://jsmentors.com/pipermail/jsmentors_jsmentors.com/