Closed mathiasbynens closed 6 years ago
Obviously, a large number of existing functions in ECMAScript perform various implicit conversions on their arguments. .fromString
could choose to match that pattern.
That said, there is also a large number of existing cases of TypeErrors being thrown for invalid arguments to various functions.
From a "minimizing surprise" point of view, given that the thing is called "fromString", being required to pass a string to it seems less surprising than being able to pass arbitrary objects. (Number.parseInt(true, 32) === 978894
seems quite surprising -- Number.fromString
should preserve more sanity!)
FWIW, implementation effort and execution speed (when passing strings) should be pretty much the same.
Step 1 of both algorithms is:
As @jakobkummerow points out, we could choose to not do this, and instead just throw for non-string arguments.