• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16class CustomArray extends Array {}
17
18function testSpeciesEffect() {
19    let custom = [1,2,3,4,5,6,7,8,9]
20    custom.constructor = CustomArray;
21
22    print("Testing methods affected by constructor:");
23
24    // Array.prototype.concat
25    let result = custom.concat([4, 5]);
26    print("concat:", result instanceof CustomArray); // true
27
28    // Array.prototype.map
29    result = custom.map(x => x * 2);
30    print("map:", result instanceof CustomArray); // true
31
32    // Array.prototype.filter
33    result = custom.filter(x => x > 1);
34    print("filter:", result instanceof CustomArray); // true
35
36    // Array.prototype.slice
37    result = custom.slice(1);
38    print("slice:", result instanceof CustomArray); // true
39
40    // Array.prototype.flatMap
41    result = custom.flatMap(x => [x, x * 2]);
42    print("flatMap:", result instanceof CustomArray); // true
43
44    // Array.prototype.flat
45    result = custom.flat();
46    print("flat:", result instanceof CustomArray); // true
47
48    // Array.prototype.splice
49    result = custom.splice(1,2,3);
50    print("splice:", result instanceof CustomArray); // true
51
52    // Array.prototype.toReversed
53    result = custom.toReversed();
54    print("toReversed:", result instanceof Array); // true
55
56    // Array.prototype.toSorted
57    result = custom.toSorted();
58    print("toSorted:", result instanceof Array); // true
59
60    // Array.prototype.toSpliced
61    result = custom.toSpliced(0,1);
62    print("toSpliced:", result instanceof Array); // true
63
64    // Array.prototype.toSpliced
65    result = custom.with(0,1);
66    print("with:", result instanceof Array); // true
67}
68
69testSpeciesEffect()
70