Skip to content

Commit dced88c

Browse files
committed
docs: es2016 string methods includes
1 parent 5e525c2 commit dced88c

1 file changed

Lines changed: 86 additions & 27 deletions

File tree

  • docs/standard-built-in-objects/indexed-collections/array/prototype/accessor-methods

docs/standard-built-in-objects/indexed-collections/array/prototype/accessor-methods/includes.md

Lines changed: 86 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,95 @@ order: 11
1212

1313
# Array.prototype.includes()
1414

15-
`Array.prototype.includes()` 方法用于判断数组是否包含指定的值。
15+
⭐️ `ES2016(ES7)新特性`
1616

17-
该方法为 ECMAScript 2015 新添加的数组方法
17+
`Array.prototype.includes()` 方法用于判断数组是否包含指定的值
1818

1919
## 语法
2020

21+
语法:
22+
2123
```js
22-
arr.includes( searchValue [, fromIndex])
24+
arr.includes(searchElement [, fromIndex])
2325
```
2426

25-
| 参数 | 说明 | 类型 |
26-
| ------------- | ------------------ | ------ |
27-
| `searchValue` | 需要查找的元素值 | any |
28-
| `fromIndex` | 查找数组开始的索引 | number |
27+
类型声明:
2928

30-
**返回值:** 如果存在指定值则返回 `true`,否则返回 `false`
29+
```ts
30+
interface Array<T> {
31+
includes(searchElement: T, fromIndex?: number): boolean;
32+
}
33+
34+
interface ReadonlyArray<T> {
35+
includes(searchElement: T, fromIndex?: number): boolean;
36+
}
37+
38+
interface Int8Array {
39+
includes(searchElement: number, fromIndex?: number): boolean;
40+
}
41+
42+
interface Uint8Array {
43+
includes(searchElement: number, fromIndex?: number): boolean;
44+
}
45+
46+
interface Uint8ClampedArray {
47+
includes(searchElement: number, fromIndex?: number): boolean;
48+
}
49+
50+
interface Int16Array {
51+
includes(searchElement: number, fromIndex?: number): boolean;
52+
}
53+
54+
interface Uint16Array {
55+
includes(searchElement: number, fromIndex?: number): boolean;
56+
}
57+
58+
interface Int32Array {
59+
includes(searchElement: number, fromIndex?: number): boolean;
60+
}
61+
62+
interface Uint32Array {
63+
includes(searchElement: number, fromIndex?: number): boolean;
64+
}
3165

32-
## 示例
66+
interface Float32Array {
67+
includes(searchElement: number, fromIndex?: number): boolean;
68+
}
3369

34-
### 代码示例
70+
interface Float64Array {
71+
includes(searchElement: number, fromIndex?: number): boolean;
72+
}
73+
```
74+
75+
参数说明:
76+
77+
| 参数 | 说明 | 类型 |
78+
| :-------------- | :----------------- | :----- |
79+
| `searchElement` | 需要查找的元素值 | any |
80+
| `fromIndex` | 查找数组开始的索引 | number |
81+
82+
**返回值:** 如果存在指定值则返回 `true`,否则返回 `false`
83+
84+
## 代码示例
3585

3686
```js
37-
let foo = [1, 2, 3];
87+
const arr = [1, 2, 3];
3888

39-
foo.includes(2);
89+
arr.includes(2);
4090
// true
4191

42-
foo.includes(4);
92+
arr.includes(4);
4393
// false
4494

45-
foo.includes(3, 3);
95+
arr.includes(3, 3);
4696
// false
4797

48-
foo.includes(3, -1);
98+
arr.includes(3, -1);
4999
// true
50100

51-
let bar = [1, 2, NaN];
101+
const arr2 = [1, 2, NaN];
52102

53-
bar.includes(NaN);
103+
arr2.includes(NaN);
54104
// true
55105
```
56106

@@ -59,29 +109,32 @@ bar.includes(NaN);
59109
`Array.prototype.indexOf()` 无法查找出数组中的 NaN,而该方法可以判断出数组中是否包含 NaN。
60110

61111
```js
62-
const a = [1, 2, 3, 4, NaN, 6, NaN];
112+
const arr = [1, 2, 3, 4, NaN, 6, NaN];
63113

64-
a.includes(NaN); // true
114+
arr.includes(NaN);
115+
// true
65116
```
66117

67118
### 开始索引超限
68119

69-
如果 `fromIndex` 大于等于数组长度 ,则返回 `false` 。该数组不会被搜索。
120+
如果 `fromIndex` 大于等于 **数组长度** ,则返回 `false` 。该数组不会被搜索。
70121

71122
```js
72123
var arr = ['a', 'b', 'c'];
73124

74-
arr.includes('c', 3); // false
75-
arr.includes('c', 100); // false
125+
arr.includes('c', 3);
126+
// false
127+
arr.includes('c', 100);
128+
// false
76129
```
77130

78-
### 开始索引负值
131+
### 开始索引为负值
79132

80-
如果 `fromIndex` 为负值,计算出的索引将作为开始搜索 `searchValue` 的位置。如果计算出的索引小于 0,则整个数组都会被搜索。
133+
如果 `fromIndex` 为负值,计算出的索引将作为开始搜索 `searchElement` 的位置。如果计算出的索引小于 0,则整个数组都会被搜索。
81134

82135
```js
83-
// 数组长度是3
84-
// fromIndex是-100
136+
// 数组长度是 3
137+
// fromIndex 是 -100
85138
// computed index 是 3 + (-100) = -97
86139

87140
var arr = ['a', 'b', 'c'];
@@ -96,7 +149,9 @@ arr.includes('c', -100);
96149

97150
### 类数组通用方法
98151

99-
`includes()` 方法有意设计为通用方法。它不要求 `this` 值是数组对象,所以它可以被用于其他类型的对象(比如类数组对象)。下面的例子展示了 在函数的 `arguments` 对象上调用的 `includes()` 方法。
152+
`Array.prototype.includes()` 方法有意设计为通用方法。它不要求 `this` 值是数组对象,所以它可以被用于其他类型的对象(比如类数组对象)。
153+
154+
下面的例子展示了 在函数的 `arguments` 对象上调用的 `includes()` 方法。
100155

101156
```js
102157
(function () {
@@ -106,3 +161,7 @@ arr.includes('c', -100);
106161
// false
107162
})('a', 'b', 'c');
108163
```
164+
165+
## 参考资料
166+
167+
- [TypeScript - lib.es2016.array.include.d.ts](https://github.com/microsoft/TypeScript/blob/main/lib/lib.es2016.array.include.d.ts)

0 commit comments

Comments
 (0)