为什么javascript的charAt()带有一个字符串返回第一个字母 [英] why javascript's charAt() with a string returns the first letter

查看:76
本文介绍了为什么javascript的charAt()带有一个字符串返回第一个字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在面向对象的javascript书中做了一些练习,我注意到这一点:

I am doing some exercises in my object-oriented javascript book, I notice that this:

var a = "hello";
a.charAt('e'); // 'h'
a.charAt('adfadf'); //'h'

为什么参数中的字符串看起来被评估为charAt的整数0 ()字符串的方法?

Why is the string in the argument seemingly evaluated to the integer 0 for the charAt() method for strings?

编辑:我知道charAt()的用法通常需要一个整数,并且练习用字符串输入charAt(),而且我也知道字符串很可能先被强制转换为整数,我确认它是NaN。感谢Kendall,建议将这些缺失的信息放在正确的问题中

I was aware that the charAt()'s usage usually takes an integer, and the exercise feeds charAt() with a string, and I also was aware that the string is likely then to be coerced into an integer first, which I did verify to be NaN. Thanks Kendall, for suggesting putting this missing bit of information in the question proper

谢谢!

推荐答案

因为数字('e') NaN < any nonempty string> .charAt(NaN)只返回第一个字符。此行为是确切地在规范中列出的内容

Because Number('e') is NaN, and <any nonempty string>.charAt(NaN) just returns the first character. This behavior is exactly what is laid out in the spec:


15.5.4.4 String.prototype.charAt(pos)



<$ c时使用一个参数 pos 调用$ c> charAt 方法,执行以下步骤:

15.5.4.4 String.prototype.charAt (pos)

When the charAt method is called with one argument pos, the following steps are taken:


  1. 致电 CheckObjectCoercible 值作为其参数。

  2. S 成为调用 ToString ,将 this 值作为其参数。

  3. 职位成为 ToInteger pos )。

  4. size S 中的字符数。

  5. 如果 position < 0或位置≥大小,返回空字符串。

  6. 返回长度为1的字符串,其中包含 S 中的一个字符,即位置位置的字符,其中 S 中的第一个(最左边)字符被认为是位置0,下一个字符位于位置1,依此类推。

  1. Call CheckObjectCoercible passing the this value as its argument.
  2. Let S be the result of calling ToString, giving it the this value as its argument.
  3. Let position be ToInteger(pos).
  4. Let size be the number of characters in S.
  5. If position < 0 or position ≥ size, return the empty String.
  6. Return a String of length 1, containing one character from S, namely the character at position position, where the first (leftmost) character in S is considered to be at position 0, the next one at position 1, and so on.


第3步是问题的症结所在。 ToInteger 'e''adfadf' 0 。为什么?再次,点击规格的时间:

Step 3 is the crux of the matter. ToInteger of both 'e' and 'adfadf' is 0. Why? Again, time to hit the spec:


9.4 ToInteger



抽象操作ToInteger将其参数转换为整数数值。这个抽象操作的功能如下:

9.4 ToInteger

The abstract operation ToInteger converts its argument to an integral numeric value. This abstract operation functions as follows:


  1. number 成为调用 ToNumber

  2. 如果数字 NaN ,返回 +0

  3. 如果数字 +0 -0 +∞-∞,返回数字

  4. 返回计算结果 sign 号码)×楼层 abs number ))。

  1. Let number be the result of calling ToNumber on the input argument.
  2. If number is NaN, return +0.
  3. If number is +0, -0, +∞, or −∞, return number.
  4. Return the result of computing sign(number) × floor(abs(number)).


我们需要更深入!什么是ToNumber('e'),什么是ToNumber('adfadf')?如果您对我再一次想要感到惊讶引用规范,我正在做某事错误:

We need to go deeper! What is ToNumber('e'), and what is ToNumber('adfadf')? If you're surprised that I'm once again about to quote the spec, I'm doing something wrong:


9.3.1 应用于字符串类型的ToNumber



ToNumber 已应用to Strings将以下语法应用于输入String。如果语法不能将String解释为 StringNumericLiteral 的扩展,那么 ToNumber NaN

9.3.1 ToNumber Applied to the String Type

ToNumber applied to Strings applies the following grammar to the input String. If the grammar cannot interpret the String as an expansion of StringNumericLiteral, then the result of ToNumber is NaN.

...我不会引用 StringNumericLiteral的整个语法。因为'e''adfadf'既不是 StrDecimalLiteral 也不是 HexIntegerLiteral ,这两个值的ToNumber都是 NaN 。最后我们进行了转换:从字符串到 NaN 0 ,这使我们将链回到 charAt 位置 0 ,所以 charAt('e') charAt('adfadf')都返回 S 中最左边的字符。

...I'm not going to quote the entire grammar for StringNumericLiteral. Because 'e' and 'adfadf' are neither StrDecimalLiteral s nor HexIntegerLiteral s, ToNumber of both of those values is NaN. Finally we have the conversion: from string to NaN to 0, which brings us back up the chain to charAt: position is 0, so charAt('e') and charAt('adfadf') both return the leftmost character in S.

现在,如果这些字符串是有效的 StrNumericLiteral ,例如'0xe'' 0xadfadf'

Now, if those strings were instead valid StrNumericLiteral s, such as '0xe' and '0xadfadf':

> 'hello'.charAt('0xe')
  ""
> 'hello'.charAt('0xadfadf')
  ""

嗯,这是一个不同的不同答案的故事。

well, that's a different story for a different answer.

这篇关于为什么javascript的charAt()带有一个字符串返回第一个字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆