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


Groups > comp.lang.javascript > #8424

Re: "" + some form value

Message-ID <1562074.40Y2kUmbCD@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2011-11-19 17:09 +0100
Subject Re: "" + some form value
Newsgroups comp.lang.javascript
References <fuodc799nhk0c9ofihe817rjtkjid2cq79@4ax.com> <65Odne1L8P9dglrTnZ2dnUVZ8uydnZ2d@giganews.com>
Followup-To comp.lang.javascript

Followups directed to: comp.lang.javascript

Show all headers | View raw


Richard Cornford wrote:

> Gene Wirchenko wrote:
>> Dear JavaScripters:
>>
>>     What does "" + some form value do?  For example,
>>          ""+document.Contest.Last.value
> 
> Assuming that - document.Contest.Last.value - if a reference to the -
> value - property of a form element then the concatenation is actually
> worthless as such - value - property values are already string
> primitives. Unfortunately forced type-conversion methods often ends up
> being applied as a mystical incantation, and so in contexts where they
> actually have no effect.
> 
> Incidentally, to force type conversion to a string primitive the String
> constructor can be used without the - new - operator, as in:-
> 
> String(document.Contest.Last.value)
> 
> - Which has been both recommended against on the grounds that it could
> be confused with a miss-typed - new String(s); - and recommended as a
> self-documenting and explicit means of forcing type-conversion to a
> string. I am leaning towards the latter. Concatenating the empty string
> has been shown to be the most runtime efficient type-conversion to
> string primitive operation.

There is no consensus on what is most runtime-efficient here.  In fact, the 
results at <http://jsperf.com/string-conversion-speed> vary so much between 
implementations, runtime environments, and even consecutive tests in the 
very same runtime environment, and String() is indeed self-documenting, that 
I am going to keep my original, ECMAScript-supported approach¹ of using 
String() when I do not know the type of the value and if it(s object 
representation) has a toString() method.


PointedEars
___________
¹) the `+' operator requires more algorithmic steps for the type conversion
   than String()
-- 
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
  -- Richard Cornford, cljs, <f806at$ail$1$8300dec7@news.demon.co.uk>

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


Thread

"" + some form value Gene Wirchenko <genew@ocis.net> - 2011-11-18 15:07 -0800
  Re: "" + some form value "Richard Cornford" <Richard@litotes.demon.co.uk> - 2011-11-19 03:17 +0000
    Re: "" + some form value Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-19 17:09 +0100
    Re: "" + some form value Gene Wirchenko <genew@ocis.net> - 2011-11-20 00:07 -0800
  Re: "" + some form value Dr J R Stockton <reply1146@merlyn.demon.co.uk> - 2011-11-19 18:53 +0000

csiph-web