6

The ECMAScript specification defines an "unique global object that is created before control enters any execution context". This global object is a standard built-in object of ECMAScript, and therefore a native object.

The spec also states:

In addition to the properties defined in this specification the global object may have additional host defined properties. This may include a property whose value is the global object itself; for example, in the HTML document object model the window property of the global object is the global object itself.

So, in web-browsers, the window object is just a convenient name for the ECMAScript global object, and therefore, the window object is a native ECMAScript object.

Did I get this correctly?

3 Answers 3

6

This mostly comes down to a question of what it really means to be a "native object" or a "host object". The ECMAScript specification provides fairly abstract definitions of those terms and there is plenty of room for differing interpretations of the definitions. For example, in the definition of native object, what is the word "semantics" actually talking about. Is it just the primitive object semantics (in ES specified by the [[propName]] internal properties) or does it include application level semantics of the object. The DOM window object certainly has observable application level semantics that are not defined in the ES specification, so if those semantics are considered it can't be a "native object".

The answer is probably much simpler if you look at it as a question of implementation pragmatics. A ES engine implementer probably thinks of any object that is allocated in the ES heap and managed by the ES garbage collector to be a "native ES object". A "host object" would generally be thought of something that exists external to the ES heap and that is accessed using some sort of interoperability layer such as COM, XPCOM, or the V8 embedding API. Depending upon the implementation, the DOM window object might fall into either category. This distinction is probably more relevant to both engine implementers and host providers than any of specification level distinctions.

There will likely be further definitional clarifications in the next edition of the ES specification. There is even a proposal to eliminate the "native" and "host" object terminology: http://wiki.ecmascript.org/doku.php?id=strawman:terminology . However, it isn't clear whether such definitions really have very much practical impact.

0
2

I could (and probably will) argue that the specification does not require the global object to be a native object. The spec defines a native object as:

object in an ECMAScript implementation whose semantics are fully defined by this specification rather than by the host environment.

And the definition of a host object:

object supplied by the host environment to complete the execution environment of ECMAScript.

The host object definition could certainly be applied to window, it is an object supplied by the host environment to complete the execution environment of ECMAScript. In addition, Its semantics are not fully defined by the ECMAScript specification.

There's also the case that the ECMAScript engines that run in browsers, such as V8, TraceMonkey, etc, do not create the window object. Rather, it is provided by the DOM (constructed and inheriting from DOMWindow or Window, for example).

4
  • 1
    But the window object is the ECMAScript global object whose semantics are fully defined by the spec... (When I say window object, I don't mean the "window" global property, but the object that that property references, which is the global object.) The host environment may add additional properties to the global object (like "window", "document", "alert", etc), but its semantics are fully defined, aren't they? May 11, 2012 at 15:16
  • 1
    @ŠimeVidas: I don't think you could say that the semantics are fully defined by the ECMAScript spec. In fact, the window object's semantics are defined in the by Window Object specification, including its inheritance from the AbstractView interface. On the other hand, you asked a very good question and I'm even open to the possibility that I may be wrong. Where's Brendon Eich, when you need him? ;-)
    – Andy E
    May 11, 2012 at 15:25
  • Ok, let's back up a little. In the browsers, the window object is the ECMAScript global object, right? (Quote from the spec: "for example, in the HTML document object model the window property of the global object is the global object itself".) May 11, 2012 at 15:29
  • 1
    (Well, the possibility of either side being wrong is a standard prerequisite of any proper argument, so, it comes without saying... :)) May 11, 2012 at 15:33
0

Yes, your reasoning sounds about right. As a semi-proof, when a function is executed without explicit this ("in the global context"), its this will evaluate to window inside the function body. But this is really JSVM-specific. For example, have a look at this v8-users message (and the related discussion.) Things are a bit more complicated behind the scenes, but look approximately as you describe to the user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.