Skip to content

Commit 00fe9e0

Browse files
committed
minor
1 parent 78176b6 commit 00fe9e0

File tree

1 file changed

+30
-26
lines changed
  • 2-ui/1-document/05-basic-dom-node-properties

1 file changed

+30
-26
lines changed

2-ui/1-document/05-basic-dom-node-properties/article.md

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,39 @@ In this chapter we'll see more into what they are and their most used properties
88

99
## DOM node classes
1010

11-
DOM nodes have different properties depending on their class. For instance, element nodes corresponding to tag `<a>` have link-related properties, and those corresponding to `<input>` have input-related and so on.
11+
DOM nodes have different properties depending on their class. For instance, an element node corresponding to tag `<a>` has link-related properties, and the one corresponding to `<input>` has input-related properties and so on. Text nodes are not the same as element nodes. But there are also common properties and methods between all of them, because all classes of DOM nodes form a single hierarchy.
1212

13-
Text nodes are not the same as element nodes: they have properties of their own.
13+
Each DOM node belongs to the corresponding built-in class.
1414

15-
But there are also common properties and methods between all of them, because of the inheritance. Each DOM node belongs to the corresponding built-in class. These classes form a hierarchy.
16-
17-
The root is [EventTarget](https://dom.spec.whatwg.org/#eventtarget), that is inherited by [Node](http://dom.spec.whatwg.org/#interface-node), and other DOM nodes inherit from it.
15+
The root of the hierarchy is [EventTarget](https://dom.spec.whatwg.org/#eventtarget), that is inherited by [Node](http://dom.spec.whatwg.org/#interface-node), and other DOM nodes inherit from it.
1816

1917
Here's the picture, explanations to follow:
2018

2119
![](dom-class-hierarchy.png)
2220

2321
The classes are:
2422

25-
- [EventTarget](https://dom.spec.whatwg.org/#eventtarget) is the root "abstract" class. Objects of that class are never created. It serves as a base, so that all DOM nodes support so-called "events", we'll study them later.
26-
- [Node](http://dom.spec.whatwg.org/#interface-node) is also an "abstract" class, serving as a base for DOM nodes. It provides the core tree functionality: `parentNode`, `nextSibling`, `childNodes` and so on (they are getters). Objects of `Node` class are never created. But there are concrete node classes that inherit from it, namely: `Text` for text nodes, `Element` for element nodes and more exotic ones like `Comment` for comment nodes.
27-
- [Element](http://dom.spec.whatwg.org/#interface-element) is a base class for DOM elements. It provides element-level navigation like `nextElementSibling`, `children` and searching methods like `getElementsByTagName`, `querySelector`. In the browser there may be not only HTML, but also XML and SVG documents. The `Element` class serves as a base for more specific classes: `SVGElement`, `XMLElement` and `HTMLElement`.
28-
- [HTMLElement](https://html.spec.whatwg.org/multipage/dom.html#htmlelement) is finally the basic class for all HTML elements. It is inherited by various HTML elements:
23+
- [EventTarget](https://dom.spec.whatwg.org/#eventtarget) -- is the root "abstract" class. Objects of that class are never created. It serves as a base, so that all DOM nodes support so-called "events", we'll study them later.
24+
- [Node](http://dom.spec.whatwg.org/#interface-node) -- is also an "abstract" class, serving as a base for DOM nodes. It provides the core tree functionality: `parentNode`, `nextSibling`, `childNodes` and so on (they are getters). Objects of `Node` class are never created. But there are concrete node classes that inherit from it, namely: `Text` for text nodes, `Element` for element nodes and more exotic ones like `Comment` for comment nodes.
25+
- [Element](http://dom.spec.whatwg.org/#interface-element) -- is a base class for DOM elements. It provides element-level navigation like `nextElementSibling`, `children` and searching methods like `getElementsByTagName`, `querySelector`. In the browser there may be not only HTML, but also XML and SVG documents. The `Element` class serves as a base for more specific classes: `SVGElement`, `XMLElement` and `HTMLElement`.
26+
- [HTMLElement](https://html.spec.whatwg.org/multipage/dom.html#htmlelement) -- is finally the basic class for all HTML elements. It is inherited by various HTML elements:
2927
- [HTMLInputElement](https://html.spec.whatwg.org/multipage/forms.html#htmlinputelement) -- the class for `<input>` elements,
3028
- [HTMLBodyElement](https://html.spec.whatwg.org/multipage/semantics.html#htmlbodyelement) -- the class for `<body>` elements,
3129
- [HTMLAnchorElement](https://html.spec.whatwg.org/multipage/semantics.html#htmlanchorelement) -- the class for `<a>` elements
3230
- ...and so on, each tag has its own class that may provide specific properties and methods.
3331

3432
So, the full set of properties and methods of a given node comes as the result of the inheritance.
3533

36-
For instance, `<input>` element is of the [HTMLInputElement](https://html.spec.whatwg.org/multipage/forms.html#htmlinputelement) class:
37-
- `HTMLInputElement` itself provides input-specific properties.
38-
- It inherits the common HTML element methods (and getters/setters) from `HTMLElement` class.
39-
- Then it supports common element methods, because `HTMLElement` inherits from `Element`.
40-
- Then it supports common DOM node properties, because it inherits from `Node`.
41-
- Finally, it supports events (to be covered), because it inherits from `EventTarget`.
42-
- (and for completeness: `EventTarget` inherits from `Object`)
34+
For example, let's consider the DOM object for an `<input>` element. It belongs to [HTMLInputElement](https://html.spec.whatwg.org/multipage/forms.html#htmlinputelement) class. It gets properties and methods as a superposition of:
35+
36+
- `HTMLInputElement` -- this class provides input-specific properties, and inherits from...
37+
- `HTMLElement` -- it provides common HTML element methods (and getters/setters) and inherits from...
38+
- `Element` -- provides generic element methods and inherits from...
39+
- `Node` -- provides common DOM node properties and inherits from...
40+
- `EventTarget` -- gives the support for events (to be covered),
41+
- ...and finally it inherits from `Object`, so "pure object" methods like `hasOwnProperty` are also available.
4342

44-
To see the DOM node class name, we can remember that an object usually has the `constructor` property. It references to the class constructor, so the `constructor.name` is what we need:
43+
To see the DOM node class name, we can recall that an object usually has the `constructor` property. It references to the class constructor, and `constructor.name` is its name:
4544

4645
```js run
4746
alert( document.body.constructor.name ); // HTMLBodyElement
@@ -53,7 +52,7 @@ alert( document.body.constructor.name ); // HTMLBodyElement
5352
alert( document.body ); // [object HTMLBodyElement]
5453
```
5554

56-
We also can use `instanceof` to check the inheritance chain:
55+
We also can use `instanceof` to check the inheritance:
5756

5857
```js run
5958
alert( document.body instanceof HTMLBodyElement ); // true
@@ -63,7 +62,9 @@ alert( document.body instanceof Node ); // true
6362
alert( document.body instanceof EventTarget ); // true
6463
```
6564

66-
As we can see, DOM nodes are regular JavaScript objects. They use prototype-based classes for inheritance. That's easy to see by outputting an element with `console.dir(elem)`. There you can see `HTMLElement.prototype`, `Element.prototype` and so on.
65+
As we can see, DOM nodes are regular JavaScript objects. They use prototype-based classes for inheritance.
66+
67+
That's also easy to see by outputting an element with `console.dir(elem)` in a browser. There in the console you can see `HTMLElement.prototype`, `Element.prototype` and so on.
6768

6869
```smart header="`console.dir(elem)` versus `console.log(elem)`"
6970
Most browsers support two commands in their developer tools: `console.log` and `console.dir`. They output their arguments to the console. For JavaScript objects these commands usually do the same.
@@ -79,19 +80,20 @@ Try it on `document.body`.
7980
````smart header="IDL in the spec"
8081
In the specification classes are described using not JavaScript, but a special [Interface description language](https://en.wikipedia.org/wiki/Interface_description_language) (IDL), that is usually easy to understand.
8182
82-
The most important difference is that all properties are given with their types. For instance, `DOMString`, `boolean` and so on.
83+
In IDL all properties are prepended with their types. For instance, `DOMString`, `boolean` and so on.
8384
8485
Here's an excerpt from it, with comments:
8586
8687
```js
8788
// Define HTMLInputElement
8889
*!*
89-
// The colon means that it inherits from HTMLElement
90+
// The colon ":" means that HTMLInputElement inherits from HTMLElement
9091
*/!*
9192
interface HTMLInputElement: HTMLElement {
93+
// here go properties and methods of <input> elements
9294
9395
*!*
94-
// "DOMString" means that the property is a string
96+
// "DOMString" means that these properties are strings
9597
*/!*
9698
attribute DOMString accept;
9799
attribute DOMString alt;
@@ -158,14 +160,14 @@ alert( document.body.tagName ); // BODY
158160
159161
Is there any difference between tagName and nodeName?
160162
161-
Actually, yes, the difference is reflected in their names, but is indeed a bit subtle.
163+
Sure, the difference is reflected in their names, but is indeed a bit subtle.
162164
163165
- The `tagName` property exists only for `Element` nodes.
164166
- The `nodeName` is defined for any `Node`:
165167
- for elements it means the same as `tagName`.
166168
- for other node types (text, comment etc) it has a string with the node type.
167169
168-
So `tagName` can only be used for elements, while `nodeName` can say something about other node types.
170+
In other words, `tagName` is only supported by element nodes (as it originates from `Element` class), while `nodeName` can say something about other node types.
169171
170172
For instance let's compare `tagName` and `nodeName` for the `document` and a comment node:
171173
@@ -193,7 +195,7 @@ The browser has two modes of processing documents: HTML and XML. Usually the HTM
193195
194196
In HTML mode `tagName/nodeName` is always uppercased: it's `BODY` either for `<body>` or `<BoDy>`.
195197
196-
In XML mode the case is kept "as is", but it's rarely used.
198+
In XML mode the case is kept "as is". Nowadays XML mode is rarely used.
197199
```
198200
199201
@@ -287,7 +289,9 @@ Here's an example:
287289
</script>
288290
```
289291
290-
Unlike `innerHTML`, writing to `outerHTML` does not change the element. Instead, it replaces it as a whole in the outer context. Yeah, sounds strange, and strange it is. Take a look.
292+
**Beware: unlike `innerHTML`, writing to `outerHTML` does not change the element. Instead, it replaces it as a whole in the outer context.**
293+
294+
Yeah, sounds strange, and strange it is, that's why we make a separate note about it here. Take a look.
291295
292296
Consider the example:
293297

0 commit comments

Comments
 (0)