title: Is there an environment-agnostic way to detect Javascript Host Objects?
body: <p>I'm writing a Javascript stacktrace library. The library needs to detect wether a particular object or function was created by the programmer or was there as part of the environment (including built-in objects). Host objects are becoming a bit problematic due to their unpredictable behaviour, so I'm after an environment-agnostic way to determine if a particular object in Javascript is a host object (see <a href="http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%201999.pdf" rel="nofollow">ECMAScript 3</a> - 4.3.8). However, distinguishing host objects from native objects and primitive values is useful for programmers in other projects, particularly in browser-less environments, so I'd like to focus on that, rather than the problems host objects cause in my library or on distinguishing programmer-created objects.</p>
  
  <p>So far I've only been able to come up with solutions that depend on the environment running javascript code. For example:</p>
  
  <pre><code>// IE Only: does not implement valueOf() in Host Objects
  var isHost = (typeof obj === 'object' &amp;&amp; typeof obj.valueOf === 'undefined');
  
  // Firefox Only: Host objects have own constructor
  var isHost = (obj.constructor &amp;&amp; obj.hasOwnProperty('constructor'));
  </code></pre>
  
  <p>I noticed that jQuery's own <a href="http://api.jquery.com/jQuery.isPlainObject/" rel="nofollow">isPlainObject()</a> method is also dependent on environment, and that the logic is rather convoluted. </p>
  
  <p>Perhaps this is because thats the nature of the beast with host objects (since their behaviour is defined by the environment), but I would like to dig a bit further to see if this is possible and was wondering if somebody has run across this particular problem before and has a solution ready.</p>
  
  <p>So. Does anybody know a simple platform-independent solution to test for Host Objects? And if it runs in a browser-less environment such as Node or Rhino all the better for it.</p>
  
  <p>Possible approaches (that may not work):</p>
  
  <ul>
  <li>Testing for characteristics of host objects seems like a lost cause, given that there is no specification for their behaviour, however testing whether the object is part of the ES3 specification may be a possibility. </li>
  <li>I have tried using <code>Object.prototype.toString()</code> given that its defined quite specifically, but the results are inconclusive as some environments (namely IE) choose to return the same value for native and host objects.</li>
  <li>It may be possible to do this by checking whether the ultimate <code>constructor</code> of an object through the prototype chain really is an <code>instanceof Function</code>.</li>
  </ul>