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 en
When you look at the definition of host object — "object supplied by the host environment to complete the execution environment of ECMAScript." — it becomes pretty clear that there's no simple way to determine if an object is host or native.
Unlike native objects, host objects define internal properties (such as [[Prototype]], [[Class]], etc.) in implementation-specific way. That's because specification allows them to do this. However, there's no "MUST" requirement for host objects to implement internal behavior in implementation-specific way; it's a "MAY" type of requirement. So we can't rely on this. These objects may or may not act "weird". There's no way to tell.
There have been few attempts to detect host objects in the past, but all of them obviously rely on observations of certain environments (MSHTML DOM being one of them) — remember that host objects don't have any kind of unique pattern/trait to identify by. Peter Michaux documented most of the inferences here (take a look at "Feature Testing a Host Object" section). The infamous typeof ... == "unknown" comes from MSHTML DOM and its ActiveX -based host objects. Note that Peter talks about host objects mainly in context of browser scripting, and he narrows the checks down to "is this a host method?", "is this a host collection object", etc.
In some environments host objects don't inherit from Object.prototype (making it easy to check against), or have certain properties that throw errors (e.g. "prototype" on some "interface" objects in IE), or even throw errors themselves on access.
It might seem like you could just check if an object is one of those defined in specification, and if not, deem it as host. But that won't really help; it would only give you objects that aren't built-in. Some of these non-standard objects could still be native (meaning that they would implement usual semantics as described in specification).
Your best bet would be to test for particular behavior of your app/script, that host objects may be sensitive to. This is always the safest way. Are you planning to access something off of an object? Delete something from object? Add something to object? Test for it. See if it works. If it doesn't — you're likely dealing with host object.