title: Strange behaviour in JSON.stringify with replacer function
body: <p>Consider this:</p>
  <pre><code>var object = {date: new Date()};
  
  JSON.stringify(object, function(key, value) {
      console.log('---');
      console.log('key:', key);
      console.log('value:', value);
      console.log('value instanceof Date:', value instanceof Date);
  
      return value;
  });
  </code></pre>
  <p>As <a href="https://developer.mozilla.org/en-US/docs/Using_native_JSON" rel="noreferrer">documentation</a> says:</p>
  <blockquote>
  <p>The replacer parameter can be either a function or an array. As a function, it takes two parameters, the key and the value being stringified. The object in which the key was found is provided as the replacer's this parameter. Initially it gets called with an empty key representing the object being stringified, and it then gets called for each property on the object or array being stringified.</p>
  <p>...</p>
  <p>If you return any other object, the object is recursively stringified into the JSON string, calling the replacer function on each property, unless the object is a function, in which case nothing is added to the JSON string.</p>
  </blockquote>
  <p>But if you run the code, you will get this:</p>
  <pre class="lang-none prettyprint-override"><code>---
  key: 
  value: { date: Fri Jan 10 2014 02:25:00 GMT+0100 (CET) }
  value instanceof Date: false
  ---
  key: date
  value: 2014-01-10T01:25:00.262Z
  value instanceof Date: false
  </code></pre>
  <p>It means, that the date property was stringified before replacer function has been called. Is it a normal behaviour or am I missing something? How can I affect format of <code>Date</code> stringification without overriding it's default <code>toJSON</code> method?</p>
  <p>Thanks!</p>
  <h2>Edit</h2>
  <p>Based on responses and next research, documentation seems to be unclear at this point and <code>toJSON</code> is beeing called before the replacer function. Based on Pills's response, this snippet should do the job:</p>
  <pre><code>var object = {date: new Date };
  
  JSON.stringify(object, function(key, value) {
      if (typeof(value) === 'object') {
          for (var k in value) {
              if (value[k] instanceof Date) {
                  value[k] = value[k].getTime();
              }
          }
      }
      return value;
  });
  </code></pre>
  <h2>Edit #2</h2>
  <p>Xotic750's solution is much better than previous one.</p>