'test'.charAt('not an integer');
此代码始终返回 't'
,我猜测当 charAt()
的参数时无法转换为整数,它只返回第一个字符。
但我似乎找不到这种行为的记录。我的假设正确吗?您能给我指出记录此行为的资源吗?
最佳答案
是的,它将返回第一个位置的字符。来自 ECMAScript 5 spec
Let position be ToInteger(pos).
在 ToInteger 规范中:
Let number be the result of calling ToNumber on the input argument.
在 ToNumber 规范中:
If the grammar cannot interpret the String as an expansion of StringNumericLiteral, then the result of ToNumber is NaN.
然后返回 ToInteger:
If number is NaN, return +0.
关于javascript - 给定一个无法解析为 int 的字符串,String.charAt() 返回什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17429557/