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


Groups > comp.lang.javascript > #39164 > unrolled thread

Re: JavaScript + DOM, type system

Started byThomas 'PointedEars' Lahn <PointedEars@web.de>
First post2020-11-20 16:53 +0100
Last post2020-11-20 16:53 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.javascript

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#39164 — Re: JavaScript + DOM, type system

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2020-11-20 16:53 +0100
SubjectRe: JavaScript + DOM, type system
Message-ID<1932328.oMNUckLgyt@PointedEars.de>
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.

[toc] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web