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 A { 17 data: string[] = ['a', 'b', 'c']; 18 $_iterator() { 19 return new DIterator(this); 20 } 21} 22 23class CIterator implements Iterator<string> { 24 index = 0; 25 base: A; 26 constructor (base: A) { 27 this.base = base; 28 } 29 next(): IteratorResult<string> { 30 if (this.index >= this.base.data.length) { 31 return { 32 done: true, 33 value: undefined 34 } 35 } 36 return { 37 done: this.index >= this.base.data.length, 38 value: this.base.data[this.index++] 39 } 40 } 41} 42 43class DIterator extends CIterator { 44 constructor (base: A) { 45 super(base); 46 } 47} 48 49function main(): void { 50 let res = ""; 51 let a = new A(); 52 for (let x of a) res += x; 53 assert(res == "abc"); 54} 55