title: Get object property name as a string
body: <p>Is it possible to get the object property name as a string</p>
  
  <pre><code>person = {};
  person.first_name = 'Jack';
  person.last_name = 'Trades';
  person.address = {};
  person.address.street = 'Factory 1';
  person.address.country = 'USA';
  </code></pre>
  
  <p>I'd like to use it like this:</p>
  
  <pre><code>var pn = propName( person.address.country ); // should return 'country' or 'person.address.country'
  var pn = propName( person.first_name );      // should return 'first_name' or 'person.first_name'
  </code></pre>
  
  <p><strong>NOTE</strong>: this code is exactly what I'm looking for. I understand it sounds even stupid, but it's not.</p>
  
  <p>This is what I want to do with it.</p>
  
  <p>HTML
      </p>
  
  <pre><code>person = {};
  person.id_first_name = 'Jack';
  person.id_last_name = 'Trades';
  person.address = {};
  person.address.id_address = 'Factory 1';
  person.address.id_country = 'USA';
  
  
  extPort.postMessage
  (
    {
      message : MSG_ACTION,
      propName( person.first_name ): person.first_name
    }
  };
  </code></pre>
  
  <p>----------------------<strong>ANSWER</strong>-----------------------</p>
  
  <p>Got it thanks to ibu. He pointed the right way and I used a recursive function</p>
  
  <pre><code>var res = '';
  
  function propName(prop, value) {
      for (var i in prop) {
          if (typeof prop[i] == 'object') {
              if (propName(prop[i], value)) {
                  return res;
              }
          } else {
              if (prop[i] == value) {
                  res = i;
                  return res;
              }
          }
      }
      return undefined;
  }
  
  var pn = propName(person, person.first_name); // returns 'first_name'
  var pn = propName(person, person.address.country); // returns 'country'
  </code></pre>
  
  <p>DEMO: <a href="http://jsbin.com/iyabal/1/edit" rel="noreferrer">http://jsbin.com/iyabal/1/edit</a></p>