4

In terms of the language what do I get back when I grab an element from the DOM as such:

var obj = document.getElementById('foo');

It has properties, so I thought that maybe it might be an object literal. Using type checks I determined that it is not an object literal, it is also not an array literal.

I know what it is used for, and how to use it, just not what it is, technically speaking in terms of the language.

I ran it through this test which I call a test for an abstract object.

obj === Object(obj);

which it returned false.

I know that I can identify node elements as such

obj.nodeType === 1

but still this doesn't tell me what it is, in terms of the language (ES5). What is an element expressed in terms of the language?

Clarification

I mean the language, based on a grammar, JavaScript, the Good Parts, Chapter 2, This grammar only knows how to deal with language components, arrays, objects, etc.

nativist.bill.cutting
  • 1,292
  • 3
  • 11
  • 19

2 Answers2

5

Element is not defined in terms of ES5. It is a part of the DOM API.

Here is its definition

The Element interface represents an element in an HTML or XML document.

A language does not have to implement the ES5 specification to implement the DOM API interface, moreover - ES5 implementations can be valid and not implement Element. For example, NodeJS does server side JavaScript and does not implement Element.

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • 1
    @Bill-TheButcher-Cutting No, it is not a part of the language. DOM objects are not normal JS objects and have no complete analogs in the specification. For example `document.body in document;//false` , `document.body;// – Benjamin Gruenbaum Jul 26 '13 at 14:16
  • In terms of the language `Element` is a host object abiding to the DOM specification of Element. You can see every place it's specified [here](https://developer.mozilla.org/en-US/docs/Web/API/element#Specifications). I can only show you how it's implemented in different browsers if you'd like. – Benjamin Gruenbaum Jul 26 '13 at 14:30
3

In terms of the ECMAScript specification, DOM Elements are "host objects" provided by the browser's host environment (emphasis mine below):

ECMAScript as defined here is not intended to be computationally self-sufficient; indeed, there are no provisions in this specification for input of external data or output of computed results. Instead, it is expected that the computational environment of an ECMAScript program will provide not only the objects and other facilities described in this specification but also certain environment-specific host objects, whose description and behaviour are beyond the scope of this specification except to indicate that they may provide certain properties that can be accessed and certain functions that can be called from an ECMAScript program.

The particular properties of DOM elements are specified by the interfaces laid out in the W3C's DOM specification, not by the ECMAScript spec (although the ECMAScript spec allows for them to exist, by allowing for environment-supplied host objects).

Addendum:

You have added some clarification that your confusion stems from the fact that JavaScript uses a grammar. I'm having a hard time understanding why that causes confusion for you, but I'll try to clear things up.

JavaScript's grammar is lexical, i.e., it deals with written code. That written code is parsed (using a grammar) and the parser identifies particular expressions in the code. Those expression correspond to programmatic operations in the execution environment.

The grammar that is used to refer to a host object is identical to the grammar used to refer to a native object. In fact, a host object is an object. The only difference is that a host object can specify the behavior of its internal methods, like [[Get]] (used for property access).

apsillers
  • 112,806
  • 17
  • 235
  • 239
  • @Bill-TheButcher-Cutting Right, but like I said, they are not defined in the ES5 specification - they're defined in the DOM API (see the link in my answer). – Benjamin Gruenbaum Jul 26 '13 at 14:28
  • 1
    @Bill-TheButcher-Cutting Correct: DOM `Element`s are outside the scope of ECMAScript and are instead specified in the W3C's DOM interfaces. – apsillers Jul 26 '13 at 14:30
  • Exactly, elements are undefined in terms of ES5. It's something the implementation in the browser adds on top and is a part of another specification - just like a lot of other things like events, timeouts and more. – Benjamin Gruenbaum Jul 26 '13 at 14:32
  • @Bill-TheButcher-Cutting What do you mean by "how host objects are parsed"? They're parsed just like any other JavaScript identifier. The behavior of DOM host objects are supplied to the execution environment by the browser (the browser *creates* the JavaScript interpreter/environment, so it's free to add whatever object definitions it pleases). – apsillers Jul 26 '13 at 14:32
  • 1
    @Bill-TheButcher-Cutting I'm afraid I still don't quite understand your follow-up question. EMCAScript has a grammar; it has expressions like `MemberExpression [ ]`, e.g., to describe bracket property access. This lexical expression is parsed, the `MemberExpression` identifies an object in the execution environment, and the whole expression corresponds to an internal method operation (here, [`[[Get]]`](http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.3)). How the `[[Get]]` operation behaves on DOM elements is defined by the browser's DOM implementation. – apsillers Jul 26 '13 at 15:06
  • 1
    @Bill-TheButcher-Cutting The reason `[[Get]]` is implementation-defined is that host object may implement internal methods [however they like](http://www.ecma-international.org/ecma-262/5.1/#sec-8.6.2): "*Host objects may support these internal properties with any implementation-dependent behaviour...*" Host objects have their *internal method* behavior supplied by the host environment (e.g., the browser). In *lexical* or *grammatical* terms, host objects act exactly like any other object; they can simply define the behavior of internal methods that correspond to certain lexical expressions. – apsillers Jul 26 '13 at 15:13
  • @Bill-TheButcher-Cutting Host objects *are objects*. When used lexically in scripts, they are parsed exactly the same as native objects. Their is no difference in lexical grammar for native vs. host objects: you perform property access with periods or square braces, you assign them to variables with `=`, etc. The definitions of host objects are provided by the host environment. – apsillers Jul 26 '13 at 19:19
  • @Bill-TheButcher-Cutting `[[Get]]` is an "[internal property](http://www.ecma-international.org/ecma-262/5.1/#sec-8.6.2)" (or in this case an internal method). In this case, `[[Get]]` describes how an object gets one of its properties. (You can see [[Get]]` listed in "Table 8 - Internal Properties Common to All Objects".) Host objects can provide an implementation of `[[Get]]` that differs from the native behavior. – apsillers Jul 26 '13 at 19:20