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> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn 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+&. <2o2qwle7mok$.1b5nmztiitach$.dlg@40tude.net> 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 "JJ" 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 – ,- | | interface Node { | […] | } – or it does not inherit from Object(.prototype) – ,- | | 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: > 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) (see “DOM interface”) and (for HTML 4.01 documentsand XHTML 1.0 documents) 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.