docs(array): add at()

This commit is contained in:
Ruan YiFeng
2021-11-15 03:33:33 +08:00
parent 1a0eaa5ffc
commit 79e871c872
2 changed files with 39 additions and 11 deletions
+13
View File
@@ -437,3 +437,16 @@ str.replaceAll(regex, replacer)
上面例子中,正则表达式有三个组匹配,所以`replacer()`函数的第一个参数`match`是捕捉到的匹配内容(即字符串`123abc456`),后面三个参数`p1``p2``p3`则依次为三个组匹配。
## 实例方法:at()
`at()`方法接受一个整数作为参数,返回参数指定位置的字符,支持负索引(即倒数的位置)。
```javascript
const str = 'hello';
str.at[1] // "e"
str.at[-1] // "o"
```
如果参数位置超出了字符串范围,`at()`返回`undefined`
该方法来自数组添加的`at()`方法,目前还是一个第三阶段的提案,可以参考《数组》一章的介绍。