string.lastIndexOf(searchValue[, fromIndex])
MDN说 fromIndex
默认值等于 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
isundefined
, thelength
of the String value is assumed, so as to search all of the String....
- Call
CheckObjectCoercible
passing the this value as its argument.- Let
S
be the result of callingToString
, giving it the this value as its argument.- Let
searchStr
beToString(searchString)
.- Let
numPos
beToNumber(position)
. (If position is undefined, this step produces the valueNaN
).- If
numPos
isNaN
, let pos be+∞;
otherwise, letpos
beToInteger(numPos)
.- Let
len
be the number of characters inS
.- Let
start
min(max(pos, 0), len)
.- Let
searchLen
be the number of characters insearchStr
.- 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/