RE: explaining baseVal and SVG DOM

Thanks Erik!

It makes sense to me now. 

David

-----Original Message-----
From: Erik Dahlström [mailto:ed@opera.com] 
Sent: Wednesday, April 15, 2009 6:50 AM
To: Dailey, David P.; SVG IG List
Subject: Re: explaining baseVal and SVG DOM

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:58:55 UTC