Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you describe an element in terms of ES5 (2)?

Tags:

javascript

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.

like image 548
nativist.bill.cutting Avatar asked Feb 15 '23 18:02

nativist.bill.cutting


2 Answers

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.

like image 74
Benjamin Gruenbaum Avatar answered Feb 24 '23 05:02

Benjamin Gruenbaum


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

like image 20
apsillers Avatar answered Feb 24 '23 05:02

apsillers