title: Sorted Map Implementation
body: <p><strong>My Task</strong></p>
  
  <p>In my JavaScript code i'm often using objects to "map" keys to values so i can later access them directly through a certain value. For example:</p>
  
  <pre><code>var helloMap = {};
  helloMap.de = "Hallo";
  helloMap["en"] = "Hello";
  helloMap.es = "Hola";
  </code></pre>
  
  <p>So i build up the map object step by step in my source code using the two available notations <em>object style</em> and <em>array style</em>.</p>
  
  <p>Later i can then access the values i added through <code>helloMap["de"]</code> for example. So thats all fine if i don't have to care about the order in which the attributes has been set on the object.</p>
  
  <p>If i want to iterate the objects properties now as far as i know there is no way to ensure that i'll iterate them in the order they have been added.</p>
  
  <p><strong>Note:</strong> I can't use some wrapper object and simply hold a array in there and then use its methods to add the values so something like this:</p>
  
  <pre><code>var HelloMap = function(){
    this.myMap = [];
    this.addProperty = function(key, value){
      this.myMap.push({key: key, value: value});
    }
  }
  </code></pre>
  
  <p>or something similar won't work for me. So the solution needs to be absolutely transparent to the programmer using the object.</p>
  
  <p>That said the object i needed would be an empty object which maintains the order of the properties that were added to it. Something like this would do:</p>
  
  <pre><code>var helloMap = {};
  helloMap = getOrderAwareObject(helloMap);
  </code></pre>
  
  <p>so that every further assignment of the form <code>helloMap.xy = "foo"</code> and <code>helloMap["yz"] = "bar"</code> would be tracked in the object "in order",</p>
  
  <p><strong>Possible Solutions</strong></p>
  
  <p>Since i did not find any solution in underscore or jQuery giving me such a special object i came across the possibility of defining getters and setters for properties in JavaScript objects with <code>Object.defineProperty</code> since i can rely on <em>ECMAScript 5</em> standard i can use it.</p>
  
  <p>The Problem with this one is, that you have to know all the possible properties that can be set on the object, before they are actually set. Since if you <em>define</em> it you got to <em>name</em> it.</p>
  
  <p>What i am searching for is something like a <strong>Default Getter</strong> and <strong>Default Setter</strong> which applies on the object if no getter and setter has been defined for the property. So i could then hide the <strong>sorted map</strong> behind the object inteface.</p>
  
  <ul>
  <li>Is there already a solution for this in any framework you know?</li>
  <li>Is there a mechanism like "default getter/setter" ?</li>
  </ul>