Line 3906 of jQuery 1.7rc1 is
expando = "sizcache" + (Math.random() + '').replace('.', ''),
I don't understand the point of using + ''
. Isn't the above equivalent to
expando = ("sizcache" + Math.random()).replace('.', ''),
Join Stack Overflow to learn, share knowledge, and build your career.
Line 3906 of jQuery 1.7rc1 is
expando = "sizcache" + (Math.random() + '').replace('.', ''),
I don't understand the point of using + ''
. Isn't the above equivalent to
expando = ("sizcache" + Math.random()).replace('.', ''),
Yes you're right. Just another way of doing it.
+ ''
is to convert it to a string - it allows replace to work.
It's not the same if the text of size changed to include a .
"sizcache." + (Math.random() + '').replace('.', '') != ("sizcache." + Math.random() + '').replace('.', '')
for example.
.replace
method is called. However, you're right in that your second line of code gives an equivalent result.
– Blazemonger
Oct 27 '11 at 19:11
I think it might be a quick way to cast the returned value of Math.random()
as a character string, so it replace()
can be used.
It's not the same thing. In their version, they are getting a random number, converting it to a string by concatenating an empty string to it, removing the dot, then appendign sizcache
. Your version appends sizcache
to the random number then removes the dot.
Granted, the end result is the same, but could potentially be different with another string value, say if it contained a dot like siz.cache
.
Yes, your method will produce the same result.
The + ''
part is used to cast the number to a string. Then the replace
function is called on that string and the sizcache
string is prepended.
However, with your method you are first adding that string to the number (and converting the number to a string in the process), then calling replace
on the entire thing. Since you are just removing dots, the result is the same.