javascript - 在javascript中通过.indexOf()传递两个变量?

标签 javascript

我一直在浏览一些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/

相关文章:

javascript - 一个函数,用于计算元素的出现次数或频率并返回一个对象,但删除出现一个的元素并返回其余元素

javascript - 重写<html>内容

javascript - 如何检查 Firebase 中的数据搜索是否成功

javascript - backbone.js 事件未绑定(bind)到文本区域上的触发操作

javascript - 使用 HTML5 JS 框架进行本地存储?

javascript - Jquery JS Lint 错误但 jquery 工作

javascript - 更改错误文本 "Incorrect user ID or password."

javascript - JS 中从其他对象借用函数

javascript - 如何获取数组中元素的所有可能组合,包括顺序和长度

javascript - 如何在禁用模式下停止按钮的悬停效果