From 9f080437808c6ddabb022a1392cb050dd2dae4b2 Mon Sep 17 00:00:00 2001 From: daiwei Date: Fri, 3 Jan 2025 16:25:10 +0800 Subject: [PATCH] feat(reactivity): collection iteration should inherit iterator instance methods --- packages/reactivity/src/collectionHandlers.ts | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/packages/reactivity/src/collectionHandlers.ts b/packages/reactivity/src/collectionHandlers.ts index 048b7f38863..94c970f236e 100644 --- a/packages/reactivity/src/collectionHandlers.ts +++ b/packages/reactivity/src/collectionHandlers.ts @@ -55,22 +55,26 @@ function createIterableMethod( ) // return a wrapped iterator which returns observed versions of the // values emitted from the real iterator - return { - // iterator protocol - next() { - const { value, done } = innerIterator.next() - return done - ? { value, done } - : { - value: isPair ? [wrap(value[0]), wrap(value[1])] : wrap(value), - done, - } - }, - // iterable protocol - [Symbol.iterator]() { - return this + return extend( + // inheriting all iterator properties + Object.create(innerIterator), + { + // iterator protocol + next() { + const { value, done } = innerIterator.next() + return done + ? { value, done } + : { + value: isPair ? [wrap(value[0]), wrap(value[1])] : wrap(value), + done, + } + }, + // iterable protocol + [Symbol.iterator]() { + return this + }, }, - } + ) } }