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


Groups > comp.lang.javascript > #21276

Re: What is the point to use the setAttribute method when there is better way

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.fsmpi.rwth-aachen.de!news-1.dfn.de!news.dfn.de!fu-berlin.de!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="UTF-8"
Message-ID <1413203.QFlV7PehVf@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Organization PointedEars Software (PES)
Date Thu, 12 Sep 2013 11:34:42 +0200
User-Agent KNode/4.10.5
Content-Transfer-Encoding 8Bit
X-Face %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
Subject Re: What is the point to use the setAttribute method when there is better way
Newsgroups comp.lang.javascript
References <l0pu7u$j9e$1@dont-email.me> <2o2qwle7mok$.1b5nmztiitach$.dlg@40tude.net> <l0r215$176$1@dont-email.me>
MIME-Version 1.0
Lines 121
NNTP-Posting-Date 12 Sep 2013 11:34:50 CEST
NNTP-Posting-Host 3727d560.newsspool4.arcor-online.net
X-Trace DXC=X@iAV9ldN0Y=>bdbdS?M0Y4IUK<Cl32<Q4Fo<]lROoRQ8kF<OcfhCO[Lf0HJ8I\J]^DZm8W4\YJN\b@mF9jNikdUmA0dJ;NON0_;XN\Yebc:H_
X-Complaints-To usenet-abuse@arcor.de
Xref csiph.com comp.lang.javascript:21276

Show key headers only | View raw


Tony Johansson wrote:

> "JJ" <duh@nah.meh> skrev […]:
>> On Wed, 11 Sep 2013 16:20:23 +0200, Tony Johansson wrote:
>>> Here I have two alternative setImage1 and setImage2 to add a value to
>>> the scr attribute and the alt attribute for the dieImg object.
>>> I find the second solution setImage2  much easier then the first one.
>>> So my question is if it's any point to use the first solution where you
>>> use the setAttribute metod
>>> The result from these two function is the same.
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>> function setImage1( dieImg )
>>>          {
>>>              var dieValue = Math.floor(1 + Math.random() * 6);
>>>             dieImg.setAttribute( "src", "img/die" + dieValue + ".png" );
>>>
>>>             dieImg.setAttribute( "alt",  "die image with " + dieValue +
>>>             "
>>> spot(s)" );
>>>          } // end function setImage
>>>
>>> function setImage2( dieImg )
>>>          {
>>>              var dieValue = Math.floor(1 + Math.random() * 6);
>>>              dieImg.src = "img/die" + dieValue + ".png"
>>>
>>>             dieImg.alt = "die image with " + dieValue + " spot(s)" );
                                                                       ^
Syntax error – the result *cannot* have been the same -----------------'

>>>          } // end function setImage
>>
>> That's because an image element is an HTMLImageElement object, and is
>> based on HTMLElement object. So, HTMLImageElement is inherited with all
>> HTMLElement object properties.
>>
>> HTMLElement is inherited with all Element object properties.
>> Element is inherited with all Node object properties.

In a loose manner of speaking.  Actually, an “img” HTML element is 
*represented* in the document tree by an object that _implements_ the 
HTMLImageElement _interface_.  That interface inherits from the HTMLElement 
_interface_, and so forth.  As a result, objects implementing an interface 
have that interface’s _attributes_, and the _attributes_ of the interfaces 
that interface inherits from, and so forth, as properties (of the same 
name).

The “src” and “alt” attributes are defined by the HTMLImageElement 
interface, therefore all objects implementing that interface (must) have 
properties of the same name.

[It does not matter then that the HTMLImageElement interface inherits from 
the HTMLElement interface.

It matters that there is the inheritance chain HTMLImageElement 
→ HTMLElement → Element because that explains why those objects have a 
setAttribute() method, too: the setAttribute() method is provided by the 
Element interface.

It does not matter that the Element interface inherits from the Node 
interface (arbitrary nodes cannot have markup attributes, only *elements* 
can).]

>> Node is inherited with all Object object properties.

No, the Node interface either does not inherit from any other interface –

,-<http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-1950641247>
|
| interface Node {
|   […]
| }

– or it does not inherit from Object(.prototype) –

,-<https://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#interface-node>
| 
| interface EventTarget {
|   […]
| }
|
| interface Node : EventTarget {
|   […]
| }

– because the DOM is a *language-neutral* API and the “Object” object is 
specified in ECMAScript only.

However, DOM implementations may decide that such host objects have the 
object referred to by the initial value of Object.prototype in their 
prototype chain.  That is, they *might* inherit Object prototype properties.
*You should not rely on that.*  There are *native* objects, and there are 
*host* objects; *DOM objects are host objects.*

See also: <https://developer.mozilla.org/en-US/docs/Web/API/Node>
 
> So in this case it's no point to use setAttributes

Actually, there is seldom a need for setAttributes() with HTML documents 
because of the shorthand attributes provided by the HTML DOM APIs, for 
example (for HTML5 documents)

<http://www.w3.org/TR/2012/CR-html5-20121217/embedded-content-0.html#the-img-element> (see “DOM interface”)

and (for HTML 4.01 documentsand XHTML 1.0 documents)

<http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-17701901>

The HTML DOM APIs extend W3C DOM Level 2+ Core, respectively.  Consider that 
the (W3C) DOM is not only for HTML documents.  For example, in order to set 
an attribute in *core* XML documents (that is, e. g., not SVG and the like), 
you have to use the setAttribute…() methods because there is no other 
(reliable) way.

Please do not top-post.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

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


Thread

What is the point to use the setAttribute method when there is better way "Tony Johansson" <johansson.andersson@telia.com> - 2013-09-11 16:20 +0200
  Re: What is the point to use the setAttribute method when there is better way JJ <duh@nah.meh> - 2013-09-12 05:05 +0700
    Re: What is the point to use the setAttribute method when there is better way "Tony Johansson" <johansson.andersson@telia.com> - 2013-09-12 02:31 +0200
      Re: What is the point to use the setAttribute method when there is better way Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-09-12 11:34 +0200

csiph-web