Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #8416 > unrolled thread
| Started by | Gene Wirchenko <genew@ocis.net> |
|---|---|
| First post | 2011-11-18 15:07 -0800 |
| Last post | 2011-11-19 18:53 +0000 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.javascript
"" + 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
| From | Gene Wirchenko <genew@ocis.net> |
|---|---|
| Date | 2011-11-18 15:07 -0800 |
| Subject | "" + some form value |
| Message-ID | <fuodc799nhk0c9ofihe817rjtkjid2cq79@4ax.com> |
Dear JavaScripters:
What does "" + some form value do? For example,
""+document.Contest.Last.value
Won't document.Contest.Last.value be string regardless? I tried
forcing one such to a number, and after the assignment, typeof()
reported it was a string.
Is this ""+ a bit of superstition, or am I missing something?
Sincerely,
Gene Wirchenko
[toc] | [next] | [standalone]
| From | "Richard Cornford" <Richard@litotes.demon.co.uk> |
|---|---|
| Date | 2011-11-19 03:17 +0000 |
| Message-ID | <65Odne1L8P9dglrTnZ2dnUVZ8uydnZ2d@giganews.com> |
| In reply to | #8416 |
Gene Wirchenko wrote: > Dear JavaScripters: > > What does "" + some form value do? For example, The addition operator does string concatenation if either of its argument is a string primitive, as in this case. However, when it does string concatenation and non-string argument (the right hand side in this case) is type-converted into a string primitive (by applying the language's type conversion rules). It is this type-conversion that usually motivates the concatenation of an empty string, that is, the result is just the result of type converting the right hand side into a string primitive, as concatenating an empty string has no effect on that result. > ""+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. > Won't document.Contest.Last.value be string regardless? Yep. > I tried forcing one such to a number, How? Strings can be type-converted into number primitive values using, say, +string or Number(string) (and commonly in the past or string*1 , or string-0 ). > and after the assignment, typeof() > reported it was a string. > > Is this ""+ a bit of superstition, or am I missing something? No, it appears to be superstition in this case. Though there is a slight possibility that - document.Contest.Last - was not referring to a form element in this case (then you only have evidence of poor code design rather than a mystical incantation). Richard.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2011-11-19 17:09 +0100 |
| Message-ID | <1562074.40Y2kUmbCD@PointedEars.de> |
| In reply to | #8420 |
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>
[toc] | [prev] | [next] | [standalone]
| From | Gene Wirchenko <genew@ocis.net> |
|---|---|
| Date | 2011-11-20 00:07 -0800 |
| Message-ID | <64dhc7h1es0b15ocbap2l7akkpc85kso7s@4ax.com> |
| In reply to | #8420 |
On Sat, 19 Nov 2011 03:17:21 -0000, "Richard Cornford"
<Richard@litotes.demon.co.uk> wrote:
>Gene Wirchenko wrote:
[snip]
>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
I think I will, too.
>has been shown to be the most runtime efficient type-conversion to
>string primitive operation.
>
>> Won't document.Contest.Last.value be string regardless?
>
>Yep.
>
>> I tried forcing one such to a number,
>
>How? Strings can be type-converted into number primitive values using,
>say, +string or Number(string) (and commonly in the past or
>string*1 , or string-0 ).
With an assignment statement. The following statement was an
alert() to get the typeof() and reported string.
[snip]
>> Is this ""+ a bit of superstition, or am I missing something?
>
>No, it appears to be superstition in this case. Though there is a slight
>possibility that - document.Contest.Last - was not referring to a form
>element in this case (then you only have evidence of poor code design
>rather than a mystical incantation).
No, definitely a form element. That is why I was puzzled about
it.
Sincerely,
Gene Wirchenko
[toc] | [prev] | [next] | [standalone]
| From | Dr J R Stockton <reply1146@merlyn.demon.co.uk> |
|---|---|
| Date | 2011-11-19 18:53 +0000 |
| Message-ID | <b$EZ2zJss$xOFw6N@invalid.uk.co.demon.merlyn.invalid> |
| In reply to | #8416 |
In comp.lang.javascript message <fuodc799nhk0c9ofihe817rjtkjid2cq79@4ax.
com>, Fri, 18 Nov 2011 15:07:56, Gene Wirchenko <genew@ocis.net> posted:
> What does "" + some form value do? For example,
> ""+document.Contest.Last.value
>Won't document.Contest.Last.value be string regardless?
Not inevitably.
document.Contest = {} ;
document.Contest.Last = {} ;
document.Contest.Last.value = new Date() ;
99 ;
document.Contest.Last.value.getHours() ;
gives me 18 (YMMV)
> I tried
>forcing one such to a number, and after the assignment, typeof()
>reported it was a string.
>
> Is this ""+ a bit of superstition, or am I missing something?
It ensures a string value; for example, this
var U ; typeof ( ''+U) // gives 'string'
typeof (+''+U) // gives 'number'
typeof (''+ +U) // gives 'string'
Some coders find it easier to force a string rather than to become
confident that it is and always will be inevitably a string already.
--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
Website <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web