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 18// 测试受 `constructor` 属性影响的方法 19function testConstructorEffect() { 20 let custom = new CustomArray(1, 2, 3); 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} 49 50function testArrayprototypechange() { 51 print("Testing array when Array's prototype changed.") 52 let newProto = {}; 53 Object.defineProperty(newProto, '1', { 54 get: function() { 55 return 1; 56 } 57 }) 58 Array.prototype.__proto__ = newProto; 59 let a = new Array(10); 60 print(a[1]); 61} 62 63testConstructorEffect() 64 65testArrayprototypechange()