Yesterday I was answering a question on stackoverflow, and there's something I don't understand in my own answer...
References: the thread in question, and my fiddle
Here is the code from my answer:
var rx = /{([0-9]+)}/g;
str=str.replace(rx,function(0,ドル1ドル){return params[parseInt(1ドル)];});
Now, what surprises me is that the following code works too:
var rx = /{([0-9]+)}/g;
str=str.replace(rx,function(0,ドル1ドル){return params[1ドル];});
My question: how come parseInt is not needed? At what point does JavaScript convert 1ドル into a number? Is it in the regex, or in the array?
1 Answer 1
That is because javascript reads indexes as a string
array[1], will be converted to array['1'] before being read
The same way object['first'] will work
1 Comment
Explore related questions
See similar questions with these tags.
typeof?ArrayasObject, but with special treatment to keys that only contains non-negative numbers.