W3C home > Mailing lists > Public > public-svg-ig@w3.org > April to June 2009

Re: explaining baseVal and SVG DOM

From: Erik Dahlström <ed@opera.com>
Date: Wed, 15 Apr 2009 12:49:41 +0200
To: "Dailey, David P." <david.dailey@sru.edu>, "SVG IG List" <public-svg-ig@w3.org>
Message-ID: <op.usfcc3h7gqiacl@gnorps.linkoping.osa>
On Fri, 10 Apr 2009 18:40:17 +0200, Dailey, David P. <david.dailey@sru.edu> wrote:

> In working through Erik D's comments upon the book I'm working on, he
> observes how an
> alternative to document.getElementById("R").getAttribute("height") might
> be document.getElementById("R").height.baseVal.value
>
> I confess to being a bit ignorant of any reasons for preferring or not
> the use of baseVal, but am considering including text such as the
> following to describe the situation:

The difference is that getAttribute returns a DOMString, and height.baseVal.value returns a float. Now, the conversion between string and float may not be such an issue in most cases, but if you do it often then those extra float <-> string conversions can become costly.

Another proposal that the SVG WG has discussed is to make e.g rect.height be essentially the same as rect.baseVal.height.value. Then you could write things like this:

circle.cx = 4; // float assignment
circle.cy = "2em"; // string assignment (sets unit and value as if valueAsString had been assigned to instead)
alert(circle.cx); // alerts whatever cx stringifies as (e.g "2em" or "4")
var diameter = circle.r * 2; // should get the float value

Cheers
/Erik

-- 
Erik Dahlstrom, Core Technology Developer, Opera Software
Co-Chair, W3C SVG Working Group
Personal blog: http://my.opera.com/macdev_ed
Received on Wednesday, 15 April 2009 10:51:54 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Wednesday, 15 April 2009 10:51:55 GMT