javascript - lastIndexOf 的默认值是多少?

标签 javascript string indexof

string.lastIndexOf(searchValue[, fromIndex])

MDNfromIndex 默认值等于 string.length,但是,我真的认为它是 string.length-1

但我怎么想并不重要...我需要有人确认 fromIndex 的默认值是什么

他们是这么说的:

"It can be any integer between 0 and the length of the string. The default value is the length of the string."

最佳答案

根据 ECMAScript 5,它将是字符串的长度

15.5.4.8 String.prototype.lastIndexOf (searchString, position)

If position is undefined, the length of the String value is assumed, so as to search all of the String.

...

  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 searchStr be ToString(searchString).
  4. Let numPos be ToNumber(position). (If position is undefined, this step produces the value NaN).
  5. If numPos is NaN, let pos be +∞; otherwise, let pos be ToInteger(numPos).
  6. Let len be the number of characters in S.
  7. Let start min(max(pos, 0), len).
  8. Let searchLen be the number of characters in searchStr.
  9. Return the largest possible nonnegative integer k not larger than start such that k+ searchLen is not greater than len, and for all nonnegative integers j less than searchLen, the character at position k+j of S is the same as the character at position j of searchStr; but if there is no such integer k, then return the value -1.

关于javascript - lastIndexOf 的默认值是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9337795/

相关文章:

javascript - Ajax.BeginForm OnBegin 确认 Via jquery modal

javascript - PHP PDO - 将动态字符串数组传递给 JavaScript

C# - 在另一个字符串中查找一组字符串中的一个的最快方法

c - 如何正确地将字符串数组作为函数参数传递?

javascript - 调用字符串时出错 :indexOf

javascript - 如何将从评估返回的变量传递给Nightmare.js中的另一个评估

javascript - 尝试导入错误: '...' is not exported from '...' error after updating 'react-scripts' to version 2. 0.0

java - 用indexof解析短语,然后获取单词的开头

javascript - 如何使用 jquery/javascript 恢复输入字段内值的更改

javascript - 为什么 "[[1, 2], [3, 4]].indexOf([1, 2])"返回-1?