我一直在浏览一些js源代码,发现有人使用indexOf()方法。但是,他们没有传递单个参数,而是传递了两个参数,如下所示:
string.indexOf(a,5)
其中“a”是预定义的字符串。这是什么意思?它只是索引多个参数吗?那么,它返回什么?
最佳答案
来自MDN docs :
arr.indexOf(searchElement[, fromIndex = 0])
其中 fromIndex
定义为:
The index to start the search at. If the index is greater than or equal to the array's length, -1 is returned, which means the array will not be searched. If the provided index value is a negative number, it is taken as the offset from the end of the array. Note: if the provided index is negative, the array is still searched from front to back. If the calculated index is less than 0, then the whole array will be searched. Default: 0 (Entire array is searched).
因此 string.indexOf(a,5)
将从索引 5< 的字符开始在
.string
中查找 a
/
关于javascript - 在javascript中通过.indexOf()传递两个变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25922249/