Iterator

SImple iterator for javascript objects and arrays, for ECMA-262 5th edition. Uses Object.keys,

by queryj

CSS

body {font-family:monospace; font-size:9pt;}
.head {color:#00f;}

JavaScript

function log() {
    var a=document.createElement("div");
    a.innerHTML=Array.prototype.slice.call(arguments).join(' ');
    document.body.appendChild(a);
    return a;
}

  function Iterator(input,keys) {
    // Input:
    //  input : object|array
    //  keys   : array|undefined|boolean
    function my() {
      ++my.index;
      if (my.index >= my.keys.length) {
        my.index = my.keys.length -1;
        my.key = my.value = undefined;
        return false;
      }
      my.key = my.useIndex ? my.index : my.keys[my.index];
      my.value = my.input[my.key];
      return my.index < my.keys.length;
    }
    if (input === null || typeof input !== 'object') {
      throw new TypeError("'input' should be object|array");
    }
    if (
      !Array.isArray(keys)
      && (typeof keys !== 'undefined')
      && (typeof keys !== 'boolean')
      ) {
      throw new TypeError("'keys' should be array|boolean|undefined");
    }
    // Save a reference to the input object.
    my.input = input;
    if (Array.isArray(input)) {
      //If the input is an array, set 'useIndex' to true if
      //the internal index should be used as a key.
      my.useIndex = !keys;
      //Either create and use a list of own properties,
      // or use the supplied keys
      // or at last resort use the input (since useIndex is true in that
      // case it is only used for the length)
      my.keys = keys===true ? Object.keys(input) : keys || input;
    } else {
      my.useIndex = false;
      my.keys = Array.isArray(keys) ? keys : Object.keys(input);
    }
    // Set index to before the first element.
    my.index = -1;
    return my;
  }
  //------Test code
  function Person(firstname, lastname, domain) {
    this.firstname = firstname;
    this.lastname = lastname;
    this.domain = domain;
  }
  Person.prototype.type = 'Brillant';

  var list = [
    new Person('Paula','Bean','some.domain.name'),
    new Person('John','Doe','another.domain.name'),
    new...