On Mon, Dec 13, 2010 at 1:53 PM, Leonardo Braga <[email protected]>wrote:
> function Loopr(_arr) {
> this.array = _arr;
> this.position = -1;
>
> this.next = function() {
> (!(++this.position < this.array.length)) && (this.position =
> 0);
> return this.array[this.position];
> }
>
I would move the next and previous methods to the prototype object. It's
not necessary for each instance to have a copy of these methods, so save
some memory and put them in the prototype object.
Also, because your properties are public, you might consider naming them
with leading underscores. It's just a convention to tell others that they
should not be accessing the properties directly. In other words, they are
public but should be treated as private. For example:
function Loopr(_arr) {
this._array = _arr;
this._position = -1;
}
Loopr.prototype = {
next: function () {
(!(++this._position < this._array.length)) && (this._position = 0);
return this._array[this._position];
},
previous: function () {
(!(--this._position > -1)) && (this._position = this._array.length -
1);
return this._array[this._position];
}
}
>
>
--
You received this message because you are subscribed to the Google Groups "The
JSMentors JavaScript Discussion Group" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/jsmentors?hl=en.