Pre and Post operators

http://stackoverflow.com/questions/22123467/cases-where-to-use-pre-increment-or-post-increment-operators

by Eduardo GarcĂ­a Sanz

JavaScript

var List = function(values) {
    
    this.index = 0;
    this.values = values;
};

List.prototype.next = function() {
  
    return this.values[++this.index];
};

List.prototype.prev = function() {
  
    return this.values[--this.index];
};

List.prototype.current = function() {
  
    return this.values[this.index];
};

List.prototype.prefix = function(prefixes) {
  
    var i;
    
    for (i = 0; i < this.values.length; i++) {
        
        this.values[i] = prefixes[i] + this.values[i];
    }
};

var animals = new List(['dog', 'cat', 'parrot']);

console.log('current', animals.current());
console.log('next', animals.next());
console.log('next', animals.next());
console.log('current', animals.current());
console.log('prev', animals.prev());
console.log('prev', animals.prev());

animals.prefix(['Snoopy the ', 'Gartfield the ', 'A ']);

console.log('current', animals.current());