Lee wrote:
Quote:
B. Fletcher said:
snip
Also, this line:
return parseInt(String.valueOf(randoms[r++]));
seems to be explicitely converting a number to a string,
and then converting it back to a number again. I'm
curious as to why that seems like a good idea. |
I am not so sure that is what it is doing. The - valueOf - method that
is being called is the method of the String function (the String object
constructor) and while the - valueOf - method of a String object
instance should return the string primitive value of that string, surly
the - valueOf - method of the - String - constructor is the one
inherited through - Function.prototype -. And as - Function.prototype -
has no - valueOf - property of its own the method called will be the one
from - Object.prototype -, which is specified as returning - this -; the
String constructor function in this case.
However, none of those - valueOf - methods take an argument anyway.
If - String.valueOf( whatever ) - returns a reference to a function
then - parseInt - will type-convert its argument to a string, which will
not resemble a number. And so - parseInt - will return - NaN -, which is
a number (sort of) but maybe not in the context of the
Array.prototype.sort method.
I recall a thread about 6 months ago in which IE producing a "Number
expected" error from Array.prototype.sort was discussed, but I cannot
remember the conclusion. Groups.google.com might be able to turn
something up (assuming they haven't completely messes google groups up
by now).
Richard.