最近刷letcode碰到的小问题
indexOf
1 | let str = "string" |
mdn解释
The index of the first occurrence of searchValue, or -1 if not found.
An empty string searchValue produces strange results. With no fromIndex value, or any fromIndex value lower than the string’s length, the returned value is the same as the fromIndex value:
1 | 'hello world'.indexOf('') // returns 0 |
如果是空的,返回传入的序列,再往下需要查看js编译器源码了
includes
传入空字符串,返回true
1 | let str = "string" |
includes,polyfill,目测includes也是通过indexOf实现的
1 | if (!String.prototype.includes) { |