javascript - In web-browsers, is the window object a native ECMAScript object? - Stack Overflow

The ECMAScript specification defines an "unique global object that is created before control enter

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?

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?

Share Improve this question asked May 11, 2012 at 14:19 Šime VidasŠime Vidas 186k65 gold badges289 silver badges391 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

This mostly es 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/doku.php?id=strawman:terminology . However, it isn't clear whether such definitions really have very much practical impact.

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 plete 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 plete 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).

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 plicated behind the scenes, but look approximately as you describe to the user.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745402790a4626177.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信