1/* 2 * Copyright (c) 2024-2025 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 AIterator implements Iterator<string> { 17 index = 0; 18 base: A 19 constructor (base: A) { 20 this.base = base; 21 } 22 next(): IteratorResult<string> { 23 return { 24 done: this.index >= this.base.data.length, 25 value: this.index >= this.base.data.length ? undefined : this.base.data[this.index++] 26 } 27 } 28} 29 30class A { 31 data: string[] = ['t', 'e', 's', 't']; 32 33 protected $_iterator() { 34 return new AIterator(this); 35 } 36} 37 38class B extends A { 39 $_iterator() { 40 return super.$_iterator(); 41 } 42} 43 44function main(): int { 45 let b = new B; 46 let idx = 0; 47 for(let it of b) { 48 assertEQ(it, b.data[idx]) 49 ++idx; 50 } 51 52 idx = 0; 53 for(let it of new B) { 54 assertEQ(it, b.data[idx]) 55 ++idx; 56 } 57 58 return 0; 59} 60