Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #39164

Re: JavaScript + DOM, type system

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: JavaScript + DOM, type system
Date 2020-11-20 16:53 +0100
Organization PointedEars Software (PES)
Message-ID <1932328.oMNUckLgyt@PointedEars.de> (permalink)
References <DOM-20201119215052@ram.dialup.fu-berlin.de>

Show all headers | View raw


Stefan Ram wrote:

>   When "a" is an element,
> 
> a.textContent = 4
> 
>   seems to behave like
> 
> a.textContent = "4"
> 
>   , but is there any specification giving the meaning of the
>   assignment of a number of JavaScript to the textContent
>   attribute of the DOM?

As it is a property that is an implementation of an attribute that is 
specified to be of type DOMString, its values have to be of a DOMString-
compatible type (see other answers).  In ECMAScript implementations this 
type is typically String.  So there is typically a setter on the property 
that converts the value to String (as one can see in the Chrome Dev Tools).
AFAIK, this behavior exists since the beginning (Netscape 2.0), even before 
setters were formally introduced into the language.

It happens that the string representation of the Number value 4 (the result 
of its conversion to String using the “String” function) is "4".

But that is nothing to be relied upon.  When in doubt, use a value that is 
already likely compatible to the expected type; in the simplest case, read 
the property before assigning to it, and use the type of the retrieved value 
as basis for choosing the type of the assigned value.

Note that properties which hold references to objects can have the primitive 
value “null”, of the Null type, as value as well.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next | Find similar | Unroll thread


Thread

Re: JavaScript + DOM, type system Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-11-20 16:53 +0100

csiph-web